Comment On More Entropy, Please

As we learned in Random Stupidity, developers don't really trust rand(), random(), Random.GetNext(), etc. Nor should they. The documentation, after all, clearly states that the function "generates a pseudo-random number." That's right, pseudo. Who wants pseudo? [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: More Entropy, Please

2008-08-11 08:05 • by GettinSadda
But... rand() & 16 is either 0 or 16!

Re: More Entropy, Please

2008-08-11 08:05 • by TheRider
Does a higher entropy mean more ordered? or less ordered? Ouch, I'm getting chaos in my head. This looks all random to me.

Re: More Entropy, Please

2008-08-11 08:07 • by snoofle
211125 in reply to 211123
GettinSadda:
But... rand() & 16 is either 0 or 16!
Agreed - shouldn't that be: & 15 ???

Re: More Entropy, Please

2008-08-11 08:07 • by tlg
Wonderful!

Re: More Entropy, Please

2008-08-11 08:09 • by TheRider
211127 in reply to 211123
GettinSadda:
But... rand() & 16 is either 0 or 16!
Winner!
But the 0 and 16 are completely random

Re: More Entropy, Please

2008-08-11 08:09 • by Quango
211128 in reply to 211124
TheRider:
Does a higher entropy mean more ordered? or less ordered? Ouch, I'm getting chaos in my head. This looks all random to me.


Yes
:-)

Re: More Entropy, Please

2008-08-11 08:15 • by TheRider
This reminds me of the much easier implementation I have seen somewhere (but, alas, can't remember where):

int function random() {

//definite random value as defined by a dice roll
return 4;
}

Re: More Entropy, Please

2008-08-11 08:19 • by monkey_code_hack (unregistered)
211130 in reply to 211129
XKCD
http://xkcd.com/221/

Re: More Entropy, Please

2008-08-11 08:19 • by tag (unregistered)

Re: More Entropy, Please

2008-08-11 08:19 • by ahnfelt
http://xkcd.com/221/ of course ;-)

EDIT: Ok, beaten to it twice!

Re: More Entropy, Please

2008-08-11 08:25 • by Bernie (unregistered)
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.

Re: More Entropy, Please

2008-08-11 08:25 • by Brompot (unregistered)
The interesting thing is, if you give it the same seed, it will produce the same 'random' number each time. Now for a randomizer for the seed ;-)

Re: More Entropy, Please

2008-08-11 08:26 • by Vollhorst (unregistered)
211135 in reply to 211132
ahnfelt:
http://xkcd.com/221/ of course ;-)

EDIT: Ok, beaten to it twice!
And that frightens me the most...

Re: More Entropy, Please

2008-08-11 08:27 • by krupa (unregistered)
But... how can you ever get enough entropy when it's always increasing in the universe? Shouldn't that be an infinite loop?

Re: More Entropy, Please

2008-08-11 08:28 • by tragomaskhalos (unregistered)
211137 in reply to 211129
TheRider:
This reminds me of the much easier implementation I have seen somewhere (but, alas, can't remember where):

int function random() {

//definite random value as defined by a dice roll
return 4;
}


In Oceans13.dll (http://www.imdb.com/title/tt0496806/) ?

Re: More Entropy, Please

2008-08-11 08:37 • by Grovesy
211138 in reply to 211132
ahnfelt:
http://xkcd.com/221/ of course ;-)

EDIT: Ok, beaten to it twice!


I think we have a new record!

The time between a new WTF article and first reference to XKCD...




Re: More Entropy, Please

2008-08-11 08:38 • by DaveAronson
Just in case some people may not be clear on the need for "random" numbers to be as close to really random as possible:

In most applications, like Windows solitaire or even playing good ol' rogue, it doesn't really matter. BUT, I'm sure many of us could think of scenarios where someone could take your money/secrets/whatever by predicting the "random" number. For example, generating crypto keys, or dealing cards in an online gambling site. In such cases, you don't want to settle for plain old "rand() seeded with the time". Most trivial psuedo-random-number generators (PRNGs) are so mechanistic that given a few outputs, the next output can be predicted with high confidence. Usually it's a simple function of the previous value. A more-random (i.e., closer to truly random) generator will incorporate additional "entropy" (no I'm not going to explain that). There are assorted tests for how random a RNG/PRNG's output is.

Re: More Entropy, Please

2008-08-11 08:40 • by Carlos92 (unregistered)
The worst thing is that if you happen to run code that tries to increase randomness in an environment where you have a hardware-entropy based random number generator, the code actually *consumes* the entropy!

Re: More Entropy, Please

2008-08-11 08:41 • by TheRider
211142 in reply to 211139
DaveAronson:
There are assorted tests for how random a RNG/PRNG's output is.
Yes. And these tests usually involve the statistical analysis of a set of values generated from the randomizer under scrutiny.

Re: More Entropy, Please

2008-08-11 08:43 • by Mee (unregistered)
211143 in reply to 211139
DaveAronson:
Just in case some people may not be clear on the need for "random" numbers to be as close to really random as possible:

In most applications, like Windows solitaire or even playing good ol' rogue, it doesn't really matter. BUT, I'm sure many of us could think of scenarios where someone could take your money/secrets/whatever by predicting the "random" number. For example, generating crypto keys, or dealing cards in an online gambling site. In such cases, you don't want to settle for plain old "rand() seeded with the time". Most trivial psuedo-random-number generators (PRNGs) are so mechanistic that given a few outputs, the next output can be predicted with high confidence. Usually it's a simple function of the previous value. A more-random (i.e., closer to truly random) generator will incorporate additional "entropy" (no I'm not going to explain that). There are assorted tests for how random a RNG/PRNG's output is.


In the right place I would be reading that and nodding to myself. As a reply to this piece of code, not so much, it just sounds like you are repeating something smart you said because that piece of code does not DO that.

Re: More Entropy, Please

2008-08-11 08:44 • by GettinSadda
211144 in reply to 211140
Carlos92:
The worst thing is that if you happen to run code that tries to increase randomness in an environment where you have a hardware-entropy based random number generator, the code actually *consumes* the entropy!

For this reason when I do any form of randomizing in code that may be included in such a system I add ^rand() to the new seed as this factors back in the original randomness

Re: More Entropy, Please

2008-08-11 08:45 • by TheRider
211145 in reply to 211133
Bernie:
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.
Now you're talking. Hit Google with a random query and hash the results html page. This should give you a truly random number.

Re: More Entropy, Please

2008-08-11 08:49 • by akatherder
What would improve this program?

A. More entropy
B. Less entropy
C. More randomness
D. More cowbell
E. None of the above

Re: More Entropy, Please

2008-08-11 08:51 • by moola (unregistered)
211147 in reply to 211131
tag:


why noone fills in title attribute when reposting images from xkcd

Re: More Entropy, Please

2008-08-11 08:53 • by Goofy McCheese (unregistered)
If you really want to increase the entropy: http://www.fourmilab.ch/hotbits/

Re: More Entropy, Please

2008-08-11 08:55 • by someone (unregistered)
Your still just jumping into the same random tables follwoing a well-defined path on the random number selection.

Re: More Entropy, Please

2008-08-11 08:59 • by Rnd() (unregistered)
This is just a random comment

No, this is a random comment

2008-08-11 09:13 • by techie (unregistered)
211156 in reply to 211151
Rnd():

This is just a random comment


4

Some Dilbert Randomness

2008-08-11 09:20 • by DaveShaw (unregistered)


Dave

Re: More Entropy, Please

2008-08-11 09:25 • by Anonymous Cow-Herd (unregistered)
According to Debian, there are only 32766 random numbers.

Re: More Entropy, Please

2008-08-11 09:33 • by bnt
There were some well-publicised problems with random-number generators in the past - e.g. this - but I hardly think this is the answer..! If your compiler's rand() function doesn't generate sufficiently-random numbers, get a better compiler.

Re: More Entropy, Please

2008-08-11 09:49 • by abx
211169 in reply to 211145
TheRider:
Bernie:
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.
Now you're talking. Hit Google with a random query and hash the results html page. This should give you a truly random number.
With a random query, eh? Recursive entropy.

Re: More Entropy, Please

2008-08-11 09:54 • by GooberMcNutly
211170 in reply to 211169
abx:
TheRider:
Bernie:
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.
Now you're talking. Hit Google with a random query and hash the results html page. This should give you a truly random number.
With a random query, eh? Recursive entropy.


How about just hash the latest Twitter post? Even human readers consider the Twitterstream pretty random.

Re: More Entropy, Please

2008-08-11 09:59 • by silent d (unregistered)
To generate a random hash, just hash the string "random"

Re: More Entropy, Please

2008-08-11 10:15 • by donniel
211181 in reply to 211145
TheRider:
Bernie:
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.
Now you're talking. Hit Google with a random query and hash the results html page. This should give you a truly random number.


Where do you get the query from? Obviously it has to be a dictionary word, since nonsense will always fetch the same result page (No results found).

Then you'll have to account for events like network or service outages...

...Or maybe you were kidding...

Re: More Entropy, Please

2008-08-11 10:19 • by StarlightKnight (unregistered)
211183 in reply to 211172
silent d:
To generate a random hash, just hash the string "random"


I'd rather a nice corned-beef hash...

(ha! beat that for random :P)

Re: More Entropy, Please

2008-08-11 10:21 • by TheRider
211184 in reply to 211181
donniel:
TheRider:
Bernie:
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.
Now you're talking. Hit Google with a random query and hash the results html page. This should give you a truly random number.


Where do you get the query from? Obviously it has to be a dictionary word, since nonsense will always fetch the same result page (No results found).

Then you'll have to account for events like network or service outages...

...Or maybe you were kidding...
Maybe. Who knows?

Re: More Entropy, Please

2008-08-11 10:25 • by uzytkownik
211185 in reply to 211127
They are guaranteed to be random - on a fair d20 dice.

Re: More Entropy, Please

2008-08-11 10:28 • by Markp
Whoops...I thought too much about it.

Re: More Entropy, Please

2008-08-11 10:30 • by Mee (unregistered)
211188 in reply to 211181
donniel:
TheRider:
Bernie:
Cloud computing sounds random to me; we should obviously hit the network whenever we need a random value.
Now you're talking. Hit Google with a random query and hash the results html page. This should give you a truly random number.


Where do you get the query from? Obviously it has to be a dictionary word, since nonsense will always fetch the same result page (No results found).

Then you'll have to account for events like network or service outages...

...Or maybe you were kidding...


At a minimum, the page will always be different because it includes your original string in the "No Results Found for X" - so it will always be slightly different, which should make a difference when hashed.

Or maybe you were kidding too.

Re: More Entropy, Please

2008-08-11 10:39 • by Robert S. Robbins (unregistered)
I have created a web service to get the NY Lottery numbers. That is what I use for random numbers.

Re: More Entropy, Please

2008-08-11 10:41 • by Code Dependent
I like the flow of his comments:
// Initialize randomizer
// This really doesn't have enough entropy....
// Get the val
I can see him on the quarter-mile thinking, "On the mark... rev the engine... pop the clutch!"

Re: More Entropy, Please

2008-08-11 10:44 • by Aaron (unregistered)
Or you could use a bit of atmospheric noise (like what is generating the die rolls here) to generate true random numbers.

Re: More Entropy, Please

2008-08-11 11:00 • by Thief^
In all seriousness, some people need it forced into them that calling rand() multiple times for one random number makes it LESS random, by massively shortening the time until the prng's sequence repeats.
Taking a stupidly extreme example, a prng that only produces the following two numbers in never-ending sequence when given the seed "42": 6 9 6 9 6 9 6 9...
What do you get if you decide that that's not random enough and xor together two numbers every time instead? That's got to be more random right? Look: 6^9=15 6^9=15 6^9=15 6^9=15. We go from 2 numbers repeating to 1 number!
How about xoring together a random number of numbers? More random, surely! Look: (6 numbers)(9^6^9^6^9^6) = 15 (9 numbers)(6^9^6^9^6^9^6^9^6) = 6 (9 numbers)(6^9^6^9^6^9^6^9^6) = 6 6 6 6. Lovely, it loops to the same number after the second call!

The only way to make it more random is to give it more data (entropy). e.g. take a 1024 element array of numbers and use the value from shitty-prng to step through it (using that primes trick to make sure that you go through all the numbers no matter what the step number is). Suddenly you can get a 1024 number sequence from a prng that only outputs 6 and 9!

Or you could just implement or use an existing implementation of the mersenne twister prng, with it's 2^19937 − 1 long number sequence and 64-bit output. Or another, if you need to. Whatever you do, don't try to make a prng of your own without a degree in number theory.

Re: More Entropy, Please

2008-08-11 11:19 • by jet082
As someone else mentioned about this particular block of code, giving it the same seed will always have it output the same number in return. Honestly, it's no better than a standard prng (obviously, it's much worse).

In this method, however, the number is STRICTLY defined by the number given to it as a seed. There is no other factor. This method will never produce a random number.

Why do people seem to think that constantly reseeding will guarantee something is more random than simply seeding the number generator once?

Re: More Entropy, Please

2008-08-11 11:49 • by DWalker59
Along with Hotbits, there's Random.org.

Re: More Entropy, Please

2008-08-11 11:49 • by Aris (unregistered)
211233 in reply to 211208
The stupied thing in that code is that the guy forces the implementation to reseed the generator at each call.
I'd bet my hands everywhere in the code the generator is seeded with time(NULL) or derivatives

Re: More Entropy, Please

2008-08-11 11:51 • by Grimoire
211234 in reply to 211164
bnt:
There were some well-publicised problems with random-number generators in the past - e.g. this - but I hardly think this is the answer..! If your compiler's rand() function doesn't generate sufficiently-random numbers, get a better compiler.

How is the quality of the compiler going to give you a better rand() function? rand() is just fine for simple applications. If you want something better, either implement your own (such as the previously mentioned Mersenne Twister) or use the OS supplied crypto quality PRNG.

Switching compilers for a different rand() function seems rather extreme and pointless...

Re: More Entropy, Please

2008-08-11 11:56 • by Randy Waterhouse (unregistered)
EAGCI CAQCK HYCUC PEXBU BXIUK GJRGT KKTRN CUNSW
YAVBV YQZWS UYPCS AJCRP VCAPT NROBK KALGB GESGA
KAYAC RAMUS CQUCF OPXCR HNSST TZXMF YXFXY IOYUI
RWYLZ EZPBC GJPYC JSBHF GFCMC AURZP AQMZC LDCAF
EHOUG REZSL EZRGL UHFLG VMXHL ZTPBT VGBKA HBEGT
QLSHS EBZJN GEZEM LDFAF ZWLAG MIHRO BHAUP SYQSI
EZMDD YOHEP MDLYD SKLZC ABKAV ZTUQL CUKPY NONVS
DHVPF ZHQKH TLIDM EDFZU QBPBR NPFBK XESTT YSAPD
IIPQL BVPGU JXWYZ NMOSN ZQRRK LQDMF HUNWL ZXGOE
BYBYW ALJPE WOVOG GZWJL BRGPO ROLYD QABSY YSKII
PEXKT DRTAB FBSLQ GCFSB IIBKA AJTKR CZWZK PCBIC
CNEVZ VBBAT DJCEU CFEWP VYPSX AIABQ DEEIZ DDAFE
UIOWN IZTNW IIUYB SYJTA AWEEF DIIEO NYWBV KKVDX
RMGML IFKRZ KRWOW BREKV SXUPZ PAKLE IDRPQ CXVMO
UXGQL CSDHC YAATQ ZJRKU VSXNH NQFJC UEZAV LDNOL
QNLQG BPPTZ KOSHB ZVSFF UZKUZ FGDTU OJHZZ OBPEU
POJHW LHSDM XYLHT LNZOG UCQCC QQEFU ZVJIC FUKXY
WVWID QTQPI XJLOL NECSK XRFGA ILUSJ TPEQX HGRYW
ZWGKK SYOUR YRIEX IMJDE SXTXN RETID PIZVT JNRYI
IXARB XAOHE SABMX OEBIM FYVET OOHGM PPKPG LNHAV
LTVNG SCKUK XZISE CGSKM FABQQ ICEPC ZBVVO BNQLI
BIIKB NMHGW ULWVB NEERT GQUBL JCZZO IBXQL YDXFJ
UAVRW XEABV UHMPJ YYMXY AFZYW LWZIC JDCFV YDAYE
VTMII WGGID FJJEH FPDFY GPCIU YGXYF CUYPC HLJNT
NSDWX KCNOH MUXPC RNJPL PSFOH IVSRJ MECPB AADNO

Re: More Entropy, Please

2008-08-11 12:01 • by Janis (unregistered)
211237 in reply to 211208
Only case where I'd suggest to call rand() multiple times: is that if you need 64bit number, but rand() generated 32bit number (or any other case where rand() generates less than you need).
And do something like this
a = rand()
b = rand()
result = a shl 32 + b

Or you have better solution for this? (Without writing my own prng)
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment