• Paul (unregistered) in reply to MrBester

    I've got a wheel that wants reinventing. Any takers? ;)

  • Spoe (unregistered) in reply to Eli

    Anonymous:
    ...and in American English the comma goes inside that quotation marks.

    Unless you're using the alternate style from The Chicago Manual of Style (which is indisputably American).

  • drange (unregistered) in reply to Michael Casadevall
    Michael Casadevall:
    Anonymous:
    Oh, didn't you know? That's how the Java Random class is implemented internally anyway!

    Well, Java is now open source, maybe I should look through the Random class code. Perpahs there is another WTF to be found there ;-).

    Yes, Java is going to be GPL, but this has always been open: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#next(int)

  • GW-BASIC rocks! (unregistered) in reply to Rich

    >Since x is limited to 2^16 values and f and g and j are deterministic, it doesn't matter what you do to munge them around, you're still limited.

     

    I know i only work with nondeterministic machines.  Turing and Neuman were nubs.

  • Jeff (unregistered) in reply to sf
    Anonymous:
    Anonymous:

    This is the only way to produce a reasonably secure random number in software. ...

     But some of the key points you are making is exactly what he is NOT doing, namely a counter (seed) and recent mouse input.  His elaborate hashing of the System properties is pointless because they typically never change during the life of the VM's execution. ...

    Ah, sorry, I'm not a Java programmer. I'd guessed the system properties included some constantly-changing stuff. Never mind then, please go ahead and resume normal WTFery.

  • (cs)

    AGH

    when you see this type of code you wonder what were they thinking? it makes sense to use the KISS principle.

    btw the 5 lines of code that is the *most correct* solution should work just fine....

    :P

  • (cs) in reply to Eli
    Anonymous:
    Anonymous:

    I realize this is a blog and not "The New Yorker", but talking about spelling, I would suggest the post writer would use some sort of automated spell-checker the next time.

    That sentence is a comma splice...

    It is not.  A comma splice is when two independent clauses are joined with a comma and no coordinating conjunction.  There are two independent clauses in that sentence, and they're joined with a comma and "but", which is a coordinating conjunction.

    The phrase "talking about spelling" is a participial phrase (most likely read as acting as an adverb modifying either "would suggest" or the entire following clause, though it could be read as an adjective modifying "I"), and not a clause, independent or otherwise.

    Setting off an introductory adverbial phrase with a comma is both common and preferred usage.

    --
    Michael Wojcik

     

  • doc0tis (unregistered) in reply to fivetrees
    fivetrees:

    Yep, quite right. The comma in this case belongs outside the quote. Putting it inside the quote results in a syntax error and a fatal exception in my brain. Have mercy, please.

    Meanwhile, back at the original post: "getRandomBits() returns a 32-byte array of random bites" and "in fact a proper implementation can be writting in five lines"... Bites? Writting? WTF?

    Steve

     

    Steve, you just have to do a:

    try

    {

        Steve.read(post)

    catch(e as spelling_grammerError)

    {

        Steve.Post(Retort.against(e.poster));

  • Sweet Rasberry Danish (unregistered) in reply to jokeyxero
    Anonymous:

    It becomes a reflex after a while and you just type it, especially if you immediately add a closing brace when you start your open.

     

    In VB I automatically type 'i after I type Next because I've done it so much.  ( For i = 0 to ... Next 'i )

     In VB you don't have to comment the i... just type "Next i" VB knows what you're doing.

     I just improved your productivity and increased the life of your keyboard.

  • (cs) in reply to GW-BASIC rocks!
    Anonymous:

    >Since x is limited to 2^16 values and f and g and j are deterministic, it doesn't matter what you do to munge them around, you're still limited.

     

    I know i only work with nondeterministic machines.  Turing and Neuman were nubs.

    You know his first name isn't "Von" right?

  • pacman (unregistered) in reply to foxyshadis
    foxyshadis:
    Anonymous:
    Whiskey Tango Foxtrot? Over.:

    Not to start a flame war or anything..... :>..... but explaining which block the } ends is important if you do that lame-assed start-a-block-on-the-same-line-that-you-declare-it thing.

    You know....

    if(foo) {
    bar();
    }

    Absolutely right. The opening-brace-on-the-same-line made sense in the days of printed listing - not any more. (And for consistency, one should put the closing brace at the end of the final line of code, right?)

    For Xmas, give yourselves braces that line up, and no closing "this is what this brace does" comments - if it really needs them, the block is too long, so break it down further. And ho ho ho.

    Steve

    captcha: pizza. yum.

    I know I certainly enjoy only being able to see half the code on the page in flurries of block open/closes. Why, it's like traveling back to DOS days! ... Indentation matters much more for readability than brace position ever does - and I'm not even a python user, it should just be obvious. If you don't indent well, no amount of braces will ever save you, and if you indent well, it'll be obvious at a glance what matches what.

    Anonymous:
    Anonymous:
    And in Ruby, it'll work in one line:

    def getRandomBits; Array.new(32) { rand 256 } end

    You can do it in one line of Java too:

    public static byte[] getRandomBits() { byte[] rand = new byte[32]; random.nextBytes(rand); return rand; }

    :-)

    That's because you can do this in one line in every language. Geez.

     

    Can you show me how to do it in Cobol on 1 line please? 

  • (cs) in reply to Spoe
    Anonymous:

    Unless you're using the alternate style from The Chicago Manual of Style (which is indisputably American).

    Damn, you mean my childhood friend from Wolverhampton who claimed to be descended from Mr Chicago was lying to me...?
     

  • (cs) in reply to tegla
    tegla:

    the WTF here is all the commenters who don't realise this code is OK.

    This creates a random seed from all the available enthropy sources present. It is not meant to be called multiple times. Just at the beginning, to seed the random generator. (maybe re-seeding every hour...)

    No, the WTF is people who evaluate code by assuming they know what its requirements are, rather than actually finding out.

     

  • test (unregistered) in reply to Rich
    Anonymous:
    JamesCurran:

    Your contension seems based on the idea that the value returned by one call is used as the seed for the next, so that if 12345 is followed by 23456, then that sequence will always appear in order in the values returned by rand(), with the only effect of the seed being when that sequence appears.

    And in fact, that is typically the case for pseudo-random numbers. Though the number returned may not actually be the seed itself, it will depend on the seed.

     If you restrict the range, you may appear to have numbers that aren't repetetive but you will still be caught by the number of bits the seed holds. A 16 bit seed limits you to 65536 values however you slice it. In the case of mssql server, this was actually much less.

     

    Mathematically, you could write it

    f(g1(x),g2(x+1),g2(x+2)...) == j(x)
     

    Since x is limited to 2^16 values and f and g and j are deterministic, it doesn't matter what you do to munge them around, you're still limited.

     

    Now, if you get lucky and through threading or other multitasking some function happens to also call rand() in the middle of your five-in-a-row rand() calls and updates the seed, you might get a new value. But if your system is calling rand() often enough that that would make any real difference, you probably need a redesign (not to mantion that that is an action-at-a-distance WTF in itself). Throwing in some entropy is a good bet too.

     

    Rich 

    Of course, the size of the seed is not necessarily the same as the size of the output, which was part of the original poster's assumption. One can generate booleans with Mersenne Twister or Blum-Blum-Shub, in which case the "for(int i=0,v=0;i<100;i++) v+=rand()%2;" method would be perfectly reasonable.

  • GW-BASIC rocks! (unregistered) in reply to rmr
    rmr:
    Anonymous:

    >Since x is limited to 2^16 values and f and g and j are deterministic, it doesn't matter what you do to munge them around, you're still limited.

     

    I know i only work with nondeterministic machines.  Turing and Neuman were nubs.

    You know his first name isn't "Von" right?

    Of course.  That only means he was some dude von the town of Neumann, silly.  I'm surprised you knew who I was talking about, as there are probably a lot of people from the town of Neumann.

  • Keith (unregistered) in reply to kipthegreat
    kipthegreat:
    Anonymous:
    Xandax:
    Anonymous:

    i like these in particular

                                } // end for
                            } // end if hash_bytes
                        } // end if val non null
                    } // end if key non null
                } // end while e.hasMoreElements

     so THAT's what }'s do...!

    Actually - I do some similar myself as it often helps my overview of the indentation, rather then having to trace an ending scope up to the starting point.
    For a looooong block of code, sure. But for code you can see all on one page? Like for a 3 line for loop? You must be kidding
    Really, those kinds of comments can be quite useful to the maintenance programmer.  Maybe not here, but it's useful sometimes when you'll have logic that looks like this:

              }
            }
          }
          else
          {

    Having a comment lets you know which if that else corresponds to.  Of course, there is usually some way to simplify the logic, but that's a different topic.. 

    Amen!

     I think making fun of the comments is a wtf in and of itself. At least they're A) there, and B) kind of useful in this case.
     

  • (cs) in reply to Pool's Closed

    Anonymous:
    The real wtf here is that I have aids AMIRITE PPL???!

    gb2/b/ tard

  • Anonymous (unregistered) in reply to Eli
    Anonymous:
    Anonymous:

    I realize this is a blog and not "The New Yorker", but talking about spelling, I would suggest the post writer would use some sort of automated spell-checker the next time.  There were enough mistakes to make the post hard to understand.

    If you're going flame someone over his or her spelling, you could at least take the time to check your grammar. Perhaps there's some sort of automated grammar checker you can use?

    That sentence is a comma splice, "spell-checker" should not be hyphenated, and in American English the comma goes inside that quotation marks.

    See what happens when you go down this road?


    Its quite common "Web Post Form" to make quotes be as literal as possible and to place all punctuation after the last quotation.
  • Martijn (unregistered) in reply to seymore15074

    This reminds me of something similar which happened a few years ago in a project I was in.

    We needed to have some very random numbers (most random number generators have a bias) in order to do something with share allocation. The algorithm for this random number generator was specified by the local financial authority and was an example of "mathemagic"; didn't know why it worked, but it did. I know this because we had been using the code for years in a COBOL implementation, we just needed to implement it in PL/1 (four years later and everything's back to COBOL again). I made the "translation" and it worked just as the original did. For some reason, however, a collegue of mine decided that the legally required algorithm wasn't good enough and somehow managed to get budget to do so. In the end, his algorithm was based on a microsecond timestamp, some rounding and, well, that's pretty much it. No matter the fact it was called multiple times per microsecond. The code was heavily biased towards some numbers (this is due to the fact the OS had more to do than just running this little program) and the number 0 was never generated due to the rounding. Oh, and did I mention his algorithm wasn't legal to use due to regulations? It was shot before ever being integrated, luckily this seems the way most WTF's are handled around here; they do happen, they just don't get through to the end.

  • Josh (unregistered) in reply to Xandax

    i like these in particular

                                } // end for
                            } // end if hash_bytes
                        } // end if val non null
                    } // end if key non null
                } // end while e.hasMoreElements

     so THAT's what }'s do...!

     

     

    Actually - I do some similar myself as it often helps my overview of the indentation, rather then having to trace an ending scope up to the starting point.

     

    You could just use a real IDE like Visual Studio. Intellisense anyone?  I worry about Java Developers. They must get paid by the amount of pain. 

     

  • Ali (unregistered) in reply to mustache

    Err... added the b, but there's still the notion of whether it's bits, bytes or bites that you really want for those 3!! I'm guessing bits.

  • (cs) in reply to Josh
    Anonymous:
    You could just use a real IDE like Visual Studio. Intellisense anyone?  I worry about Java Developers. They must get paid by the amount of pain. 


    Um... our Visual Studio is called eclipse. Does largely the same things in the same way, except it's not called "intellisense" because that's a Microsoft trademark.

     
    But the real point is that if you need comments to match braces, then your blocks are either not properly indented, or too long.
     

  • (cs) in reply to Josh

    Anonymous:
    You could just use a real IDE like Visual Studio. Intellisense anyone?

    This always strikes me as a strange point of view. The correct answer to my mind to 'my code is hard to read because my scope blocks are too big' is not 'let's use an IDE to navigate my unreadable code' but 'let's reorganise the code to the scope blocks aren't so big'.

    Code completion and the like are handy time savers, but I would never want to be in the position where I couldn't load up a text editor and read my code if necessary because I could only navigate around it with a special piece of software. 

  • iw (unregistered) in reply to fivetrees

    I also strongly disagree with how you part your hair. The part on the far right side of the head made sense in the days when we had hair slick, but nowadays it just doesn't make any sense! Do yourself a favor and part your hair on the left like a sane human being!

  • iw (unregistered) in reply to fivetrees
    Anonymous:
    Whiskey Tango Foxtrot? Over.:
    Anonymous:

    i like these in particular

                                } // end for
                            } // end if hash_bytes
                        } // end if val non null
                    } // end if key non null
                } // end while e.hasMoreElements

     so THAT's what }'s do...!

    Not to start a flame war or anything..... :>..... but explaining which block the } ends is important if you do that lame-assed start-a-block-on-the-same-line-that-you-declare-it thing.

    You know....

    if(foo) {
    bar();
    }

    Absolutely right. The opening-brace-on-the-same-line made sense in the days of printed listing - not any more. (And for consistency, one should put the closing brace at the end of the final line of code, right?)

    For Xmas, give yourselves braces that line up, and no closing "this is what this brace does" comments - if it really needs them, the block is too long, so break it down further. And ho ho ho.

    Steve

    captcha: pizza. yum.

    I also strongly disagree with how you part your hair. The part on the far right side of the head made sense in the days when we had hair slick, but nowadays it just doesn't make any sense! Do yourself a favor and part your hair on the left like a sane human being!

     
    gah why cant you edit your comments??
     

  • iw (unregistered) in reply to GW-BASIC rocks!
    Anonymous:

    >Since x is limited to 2^16 values and f and g and j are deterministic, it doesn't matter what you do to munge them around, you're still limited.

     

    I know i only work with nondeterministic machines.  Turing and Neuman were nubs.

    Hey, could you solve these graph problems for me real quick? I've got a team of nationwide door-to-door traveling salesman that I really need to get out the door as quickly as possible.

  • Sirusdv (unregistered)

    First post and a contribution:

     

     

    int RandInRange(int min, int max) { //pick a few rand numbers to warm up the number generator for(int j=0; j<100; j++) rand();

     char* i = new char[rand() % 128];
     return min + ( ((int)i) % (max-min) );
    

    }

  • Anonymous (unregistered) in reply to UMTopSpinC7
    UMTopSpinC7:

    Anonymous:
    Xandax:
    Anonymous:

    i like these in particular

                                } // end for
                            } // end if hash_bytes
                        } // end if val non null
                    } // end if key non null
                } // end while e.hasMoreElements

     so THAT's what }'s do...!

    Actually - I do some similar myself as it often helps my overview of the indentation, rather then having to trace an ending scope up to the starting point.

     

    For a looooong block of code, sure. But for code you can see all on one page? Like for a 3 line for loop? You must be kidding.

    If you are used to putting comments after }'s then you probably do it out of habit. I would say either do it or don't do it for the sake of consistency.


    Better yet, if a loop only contains one line I don't bother putting in {}.
  • Erik (unregistered) in reply to Jeff
    Anonymous:
    Anonymous:
    Anonymous:

    This is the only way to produce a reasonably secure random number in software. ...

     But some of the key points you are making is exactly what he is NOT doing, namely a counter (seed) and recent mouse input.  His elaborate hashing of the System properties is pointless because they typically never change during the life of the VM's execution. ...

    Ah, sorry, I'm not a Java programmer. I'd guessed the system properties included some constantly-changing stuff. Never mind then, please go ahead and resume normal WTFery.

    The purpose of this supposed WTF code, presuming it's being properly used, is NOT a replacement for the Random class.  It is NOT supposed to be an algorithmic PRNG.  It is an attempt to get random seed bits.  This is something that has to be done ONCE per JVM execution.  After that, you can use something like SecureRandom, seeded by this, to produce whatever further random bits you want.  The SecureRandom class claims to "attempt" to completely randomize its initial state, but it doesn't explain how, and I seriously doubt it's done in anything close to a "cryptographically secure" way.  The only thing it claims to be secure are the ALGORITHMS it encapsulates.  Anyway, the Random class definitely seeds itself with only a (non-secure) hash of the system clock, so the suggested "correct" sollution is certainly a bigger WTF than the supposed WTF.

  • Hognoxious (unregistered) in reply to Xandax

    Xandax:
    Actually - I do some similar myself as it often helps my overview of the indentation, rather then having to trace an ending scope up to the starting point.
    I do too, though I try to avoid having 98 levels of nesting to start with.

  • (cs) in reply to rmr

    rmr:
    Makes you wonder how Random.nextBytes() is implemented.

    You don't need to wonder. It uses an 48-bit linear congruential PRNG, according to the docs. The source code is available, and has been since before Sun open sourced Java. (All the source for all the Java classes always has been, but the JVM source wasn't always available.)

    Although, you can't actually call Random.nextBytes(). It isn't a class method. You have to instantiate a Random.

    For what the coder was trying to do, a java.security.SecureRandom might have been a better way to go. It's hard to tell what his or her intentions were, though. Security, or just making it "really, really random"?

  • (cs) in reply to JamesCurran
    JamesCurran:
    Anonymous:

    public static byte[] getRandomBits() { byte[] rand = new byte[32]; random.nextBytes(rand); return rand; }

    Well, assuming that "random" is a properly seeded static instance of the Random class, then the C# version is identical, except one letter would need to be capitalized.

    What's a static instance? Is that like a constant variable?

    Never mind, it was being used in a static method. I should have thought before I posted.

  • (cs) in reply to iw
    Steve:

    Absolutely right. The opening-brace-on-the-same-line made sense in the days of printed listing - not any more. (And for consistency, one should put the closing brace at the end of the final line of code, right?)

    For Xmas, give yourselves braces that line up....

    I don't understand what goes on in the head of someone who insists on this. Do you look at a closing brace, look up at the same level of indentation, find a "for" (or an "if" or a "while" or whatever), and get confused?

    If the closing brace matches up with a for, then I know it ends the for loop. An opening brace that lines up would add no additional information. Unless I was worried that someone had checked in code that had blocks with closing braces, but no opening braces. In which case the solution to the problem would not involve reformatting the code.

    And what do printed listings have to do with anything? Is concision not valuable on-screen?

    As to putting the closing brace at the end of a line, that's not consistency, that's OCD. "A foolish consistency etc."

  • Anonymous (unregistered) in reply to Whiskey Tango Foxtrot? Over.

    THANK YOU! I've been thinking the same exact thing reading these comments.

  • wikiphile (unregistered) in reply to sir_flexalot

    sir_flexalot:
    Someone should really come up with a book that is like a reference, and it just has 1000 examples, like, "to do random numbers, do this..." and then the shortest, cleanest possible way to do it.  Maybe there'd be a chance that people would stop coding so much and get something done.

    That would be great.

    Perhaps you and 1000 other WTF readers could write up one example each in, say, Wikibooks: Java ?

     

     

  • Scotty (unregistered) in reply to foxyshadis
    foxyshadis:
    That's because you can do this in one line in every language. Geez.  

    Obviously you never had to program in Fortran IV using punch cards.

  • umm (unregistered) in reply to Xandax
    Xandax:
    Anonymous:
    i like these in particular                             } // end for                        } // end if hash_bytes                    } // end if val non null                } // end if key non null            } // end while e.hasMoreElements so THAT's what }'s do...!

    Actually - I do some similar myself as it often helps my overview of the indentation, rather then having to trace an ending scope up to the starting point. 

    Umm, the % key in vi. Use it. Love it.

  • Joe (unregistered) in reply to Hunter
    bits = new byte[(nbits + 7) / 8];So he adds 7 to nbits, then divides by 8, making the addition totally superfluous. This is of course assuming that the number of bits is actually an integer, but given some of the stuff I've seen here, I woudn't be surprised if he kept track of fractions of bits. :)

    You're a moron. How many bytes do you need to hold nbits bits? It's (nbits + 7) / 8.

    8 bits = (8 + 7) / 8 = 1 byte.
    9 bits = (9 + 7) / 8 = 2 bytes.

Leave a comment on “Random Ways To Get To Random()”

Log In or post as a guest

Replying to comment #:

« Return to Article