Home  Forums  Reviews  Guides  Newsgroups  Register  Search 
Thread Tools 
Keith Thompson
Guest
Posts: n/a

"Rod Pemberton" <(EMail Removed)> writes:
> "A. Sinan Unur" <(EMail Removed)> wrote in message > news:Xns9775569186277asu1cornelledu@127.0.0.1... >> "Rod Pemberton" <(EMail Removed)> wrote in >> news:dtp4td$7tok$(EMail Removed): >> > "Keith Thompson" <(EMail Removed)> wrote in message >> > news:(EMail Removed)... >> ... >> >> Calling srand() more than once >> >> makes sense *only* if you want to repeat the same sequence. >> > >> > False. >> > >> > You apparently meant to say this: "'Calling srand() more than once' >> > _with_the_same_value_ 'makes sense *only* if you want to repeat the >> > same sequence.'" >> >> You are misreading Keith's post. Calling srand multiple times with >> different seeds during the life time of the program *decreases* the >> randomness of the sequence generated. > > False. > > What you are claiming is that the randomness of the sequence increases as > the rand() function is used. No, nobody made that claim. [snip] > By calling srand() we increased the probability of > some numbers which had low probability and reduced the probability of other > numbers which had low probability. So you're asserting that repeatedly calling srand() improves the randomness of the numbers returned by rand()? In particular, you're claming that that something like this: int rand_array[SOME_SIZE]; int index = 0; srand(some_value); rand_array[index++] = rand(); rand_array[index++] = rand(); rand_array[index++] = rand(); srand(some_other_value); rand_array[index++] = rand(); rand_array[index++] = rand(); rand_array[index++] = rand(); is likely to yield better (more random) results than if the second call to srand() were removed (perhaps with a different number of calls)? If this is correct, then there's an error in the comp.lang.c FAQ, question 13.17. If you can convince Steve Summit that this is an error, I'm sure he'll be willing to change it. You'll need to support your claim with concrete evidence, of course. It would also be interesting to know how often, under what circumstances, and with what arguments srand() should be called to get the best possible pseudorandom numbers. (I happen to think that you're completely wrong, but I've been mistaken before.) If I've misunderstand what you're claiming, please clarify.  Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(EMail Removed) <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. 




Keith Thompson 


 
CBFalconer 


 
Keith Thompson
Guest
Posts: n/a

CBFalconer <(EMail Removed)> writes:
> Keith Thompson wrote: [snip] > It could happen with particularly bad random number generators. > For example, some algorithms can get into multiple independant > cycles, of different lengths. However even Microsoft is not likely > to use these algorithms. As far as I know the sample implementation in the standard doesn't have that flaw, and there's no excuse for using an implementation worse than that.  Keith Thompson (The_Other_Keith) (EMail Removed) <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. 




Keith Thompson 
Ben Bacarisse
Guest
Posts: n/a

On Sun, 26 Feb 2006 05:33:36 +0000, Keith Thompson wrote:
>> By calling srand() we increased the >> probability of >> some numbers which had low probability and reduced the probability of >> other numbers which had low probability. > > So you're asserting that repeatedly calling srand() improves the > randomness of the numbers returned by rand()? In particular, you're > claming that that something like this: > > int rand_array[SOME_SIZE]; > int index = 0; > srand(some_value); > rand_array[index++] = rand(); > rand_array[index++] = rand(); > rand_array[index++] = rand(); > srand(some_other_value); > rand_array[index++] = rand(); > rand_array[index++] = rand(); > rand_array[index++] = rand(); > > is likely to yield better (more random) results than if the second call to > srand() were removed (perhaps with a different number of calls)? There is a degenerate case where you will almost certainly get better "randomness": by calling srand with a seed chosen by a highquality hardware RNG before every call to rand. (I say almost because the transformation from srand to the result of rand just *might* be pathologically bad.) Obviously, this case is absurd (just use your better source) but it is possible (I'll put it no more strongly than that) that in cases where the entropy of your source is precious you might get some improvement by calling srand with a new random seed for every nth call to rand (for n > 1). If we use n==0 for the case where we use our source directly, then we have a continuity from n==0 giving good randomness but using up our precious entropy, to n==inf where we use none of it and rely on rand entirely. The optimal point (if there is one) will depend on circumstances. However (and it is a big however) as someone who has worked on RNGs for cryptographic applications I would not recommend this technique at all! The reason is that the effort involved in studying the algorithm used by srand/rand to ensure that is does not introduce any serious flaws in the random number stream is just too great. If you need a very high quality stream, there are plenty of open source implementations available (with supporting analysis). If you don't them using rand (with srand called once at the start) will do just fine. Interleaving calls to srand will cause the reader to wonder if you have made things mysteriously worse, even though it might well be helping a little. In short, I agree with you that it is a Bad Idea.  Ben. 




Ben Bacarisse 
Kenneth Brody
Guest
Posts: n/a

Keith Thompson wrote:
> > "Rod Pemberton" <(EMail Removed)> writes: [...] > > By calling srand() we increased the probability of > > some numbers which had low probability and reduced the probability of other > > numbers which had low probability. > > So you're asserting that repeatedly calling srand() improves the > randomness of the numbers returned by rand()? In particular, you're > claming that that something like this: > > int rand_array[SOME_SIZE]; > int index = 0; > srand(some_value); > rand_array[index++] = rand(); [...] > srand(some_other_value); > rand_array[index++] = rand(); [...] > > is likely to yield better (more random) results than if the second > call to srand() were removed (perhaps with a different number of > calls)? [...] I know someone who did (in a language other than C) the equivalent of: int MyRandomNumber(void) { int ret; srand(ret = rand()); return(ret); } (ie: constantly reseeding the generator with the previous return value) and then complained about the lowrandomness.  ++++  Kenneth J. Brody  www.hvcomputer.com    kenbrody/at\spamcop.net  www.fptech.com  #include <std_disclaimer.h>  ++++ Don't email me at: <(EMail Removed)> 




Kenneth Brody 
Rod Pemberton
Guest
Posts: n/a

"A. Sinan Unur" <(EMail Removed)> wrote in message news:Xns9775C9E26F2F4asu1cornelledu@127.0.0.1... > "Rod Pemberton" <(EMail Removed)> wrote in > news:dtqerj$8usd$(EMail Removed): > > > > > "A. Sinan Unur" <(EMail Removed)> wrote in message > > news:Xns9775569186277asu1cornelledu@127.0.0.1... > >> "Rod Pemberton" <(EMail Removed)> wrote in > >> news:dtp4td$7tok$(EMail Removed): > >> > >> > > >> > "Keith Thompson" <(EMail Removed)> wrote in message > >> > news:(EMail Removed)... > >> > >> ... > >> > >> >> Calling srand() more than once > >> >> makes sense *only* if you want to repeat the same sequence. > >> > > >> > False. > >> > > >> > You apparently meant to say this: "'Calling srand() more than once' > >> > _with_the_same_value_ 'makes sense *only* if you want to repeat the > >> > same sequence.'" > >> > >> You are misreading Keith's post. Calling srand multiple times with > >> different seeds during the life time of the program *decreases* the > >> randomness of the sequence generated. > > > > False. > > > > What you are claiming is that the randomness of the sequence increases > > as the rand() function is used. > > That is not what I am claiming at all. You are misreading my post as > well as Keith's. Fine... > > This is patently false. A simple 2d > > plot of a pseudorandom number generator reveals that the generated > > pattern of the numbers is static. And, therefore, the randomness is > > independent of the number of calls to rand(), but dependent on the > > algorithm. > > Of course it is. But if the algorithm is hosed, how can calling srand > multiple times help? The algorithm isn't "hosed." It's just not a random number generator. It's pseudorandom. > > Since most pseudorandom number generators have > > mathematical defects, such as repeated numbers, skipped numbers, > > clustering of nonrandom numbers, etc., this means that some numbers > > have a high probability (or chance) of occuring and others have a low > > probability of occuring. Since a 2d plot of the values is static and > > we don't care what it looks like, one can visualize it using a > > chessboard or checkerboard pattern. Now, if the origin is at (0,0) > > before calling srand() and changes to (0.5,0.75) after calling > > srand(), what happened? The pattern in which the numbers are > > generated didn't change. It's still a chessboard or checkerboard or > > whatever, but the pattern is shifted. However, the _set_ of numbers > > which generate that chessboard or checkerboard did change. By calling > > srand() we increased the probability of some numbers which had low > > probability and reduced the probability of other numbers which had low > > probability. > > You have introduced a chickenandegg problem here: To get appropriately > random numbers, you are claiming, one needs to keep reseeding the random > number generator with appropriately random numbers. Huh? If I was misreading before, you are now. As I've stated previously, the randomness is in the nonperfect algorithm in rand(). But, the set of numbers generated by rand() is affected by srand(). srand() doesn't affect the randomness of values that rand() generates, it only changes the set of generated numbers. Since the algorithm isn't a perfectrandom number generator but a pseudorandom number generator, the probabilities of certain numbers occurring is higher than others. These probabilities can be shifted by calls to srand(). Rod Pemberton 




Rod Pemberton 
Rod Pemberton
Guest
Posts: n/a

"Keith Thompson" <(EMail Removed)> wrote in message news:(EMail Removed)... > "Rod Pemberton" <(EMail Removed)> writes: > > "A. Sinan Unur" <(EMail Removed)> wrote in message > > news:Xns9775569186277asu1cornelledu@127.0.0.1... > >> "Rod Pemberton" <(EMail Removed)> wrote in > >> news:dtp4td$7tok$(EMail Removed): > >> > "Keith Thompson" <(EMail Removed)> wrote in message > >> > news:(EMail Removed)... > >> ... > >> >> Calling srand() more than once > >> >> makes sense *only* if you want to repeat the same sequence. > >> > > >> > False. > >> > > >> > You apparently meant to say this: "'Calling srand() more than once' > >> > _with_the_same_value_ 'makes sense *only* if you want to repeat the > >> > same sequence.'" > >> > >> You are misreading Keith's post. Calling srand multiple times with > >> different seeds during the life time of the program *decreases* the > >> randomness of the sequence generated. > > > > False. > > > > What you are claiming is that the randomness of the sequence increases as > > the rand() function is used. > > No, nobody made that claim. > > [snip] > > > By calling srand() we increased the probability of > > some numbers which had low probability and reduced the probability of other > > numbers which had low probability. > > So you're asserting that repeatedly calling srand() improves the > randomness of the numbers returned by rand()? In particular, you're > claming that that something like this: <snip> > is likely to yield better (more random) results than if the second > call to srand() were removed (perhaps with a different number of > calls)? KEITH: NO! Completely incorrect! This is the fifth time and last time. Since I'm tied of trying to get through to you, I'll just repeat what I posted to Sinaur. If you don't comprehend, you can deal with your inabilities in private. "As I've stated previously, the randomness is in the nonperfect algorithm in rand(). But, the set of numbers generated by rand() is affected by srand(). srand() doesn't affect the randomness of values that rand() generates, it only changes the set of generated numbers. Since the algorithm isn't a perfectrandom number generator but a pseudorandom number generator, the probabilities of certain numbers occurring is higher than others. These probabilities can be shifted by calls to srand(). " Rod Pemberton 




Rod Pemberton 
Keith Thompson
Guest
Posts: n/a

"Rod Pemberton" <(EMail Removed)> writes:
> "Keith Thompson" <(EMail Removed)> wrote in message > news:(EMail Removed)... [...] >> So you're asserting that repeatedly calling srand() improves the >> randomness of the numbers returned by rand()? In particular, you're >> claming that that something like this: > <snip> >> is likely to yield better (more random) results than if the second >> call to srand() were removed (perhaps with a different number of >> calls)? > > KEITH: NO! Completely incorrect! This is the fifth time and last time. > Since I'm tied of trying to get through to you, I'll just repeat what I > posted to Sinaur. If you don't comprehend, you can deal with your > inabilities in private. > > "As I've stated previously, the randomness is in the nonperfect algorithm > in rand(). But, the set of numbers generated by rand() is affected by > srand(). srand() doesn't affect the randomness of values that rand() > generates, it only changes the set of generated numbers. Since the > algorithm isn't a perfectrandom number generator but a pseudorandom number > generator, the probabilities of certain numbers occurring is higher than > others. These probabilities can be shifted by calls to srand(). " The fact that rand() is a pseudorandom number generator does not imply that certain numbers appear with a higher probability than others. For example, if RAND_MAX==32767, it's entirely possible that each of the 32768 possible values will appear with exactly equal probability over the long run, for each possible seed. (The generator can still be imperfectly random even if this is true. For example, rand() is likely to repeat over a cycle whose length depends on the size of its internal state; a true random number generator would not do so, though over the very long run it will sometimes appear to do so.) Or have I misunderstood what you meant by "the probabilities of certain numbers occurring is higher than others"? In your initial contribution to this thread, you wrote: ] The algorithm which generates a semirandom or pseudorandom number ] sequence has some internal initial values. If you don't call ] srand(), the sequence will be semirandom but will be the same ] sequence every time you run your program. So, if you were to write ] a card playing program, you might call srand() at every shuffle to ] start a new semirandom sequence and call rand() to generate the ] deck of cards. The "randomness" comes from the algorithm in rand() ] not from the starting values in generated by srand(). My response was that it would make more sense to call, say, srand(time(NULL)) exactly once at program startup, and use successive values from the *same* pseudorandom sequence for successive shuffles. You said I was wrong. You seem to be claiming that calling srand() *again* for each shuffle is somehow better than calling srand() exactly once at program startup and generating all shuffles from the single resulting pseudorandom sequence. (By "calling srand()", I mean "calling the srand function with some appropriate argument, such as srand(time(NULL))".) Is that in fact what you've been claiming? In what sense is calling srand() repeatedly better than calling it only once? How is starting a new pseudorandom sequence better than continuing to use the original one? Note that inserting an extra call to rand() will also change the behavior of subsequent calls to rand(); would that suit your purpose as well?  Keith Thompson (The_Other_Keith) (EMail Removed) <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. 




Keith Thompson 
A. Sinan Unur
Guest
Posts: n/a

"Rod Pemberton" <(EMail Removed)> wrote in
news:du02tu$cfqa$(EMail Removed): > "Keith Thompson" <(EMail Removed)> wrote in message > news:(EMail Removed)... >> "Rod Pemberton" <(EMail Removed)> writes: >> > "A. Sinan Unur" <(EMail Removed)> wrote in message >> > news:Xns9775569186277asu1cornelledu@127.0.0.1... >> >> "Rod Pemberton" <(EMail Removed)> wrote in >> >> news:dtp4td$7tok$(EMail Removed): >> >> > "Keith Thompson" <(EMail Removed)> wrote in message >> >> > news:(EMail Removed)... >> >> ... >> >> >> Calling srand() more than once >> >> >> makes sense *only* if you want to repeat the same sequence. >> >> > >> >> > False. >> >> > >> >> > You apparently meant to say this: "'Calling srand() more than >> >> > once' _with_the_same_value_ 'makes sense *only* if you want to >> >> > repeat the same sequence.'" >> >> >> >> You are misreading Keith's post. Calling srand multiple times with >> >> different seeds during the life time of the program *decreases* >> >> the randomness of the sequence generated. >> > >> > False. >> > >> > What you are claiming is that the randomness of the sequence >> > increases as the rand() function is used. >> >> No, nobody made that claim. >> >> [snip] >> >> > By calling srand() we increased the probability of >> > some numbers which had low probability and reduced the probability >> > of other numbers which had low probability. This is nonsense. >> So you're asserting that repeatedly calling srand() improves the >> randomness of the numbers returned by rand()? In particular, you're >> claming that that something like this: > <snip> >> is likely to yield better (more random) results than if the second >> call to srand() were removed (perhaps with a different number of >> calls)? > > KEITH: NO! Completely incorrect! This is the fifth time and last > time. Since I'm tied of trying to get through to you, I'll just repeat > what I posted to Sinaur. The correct spelling of my name is 'Sinan'. > If you don't comprehend, you can deal with your inabilities in > private. It is impossible for me to comprehend what you are saying because it makes no sense. There are a number of standard texts on pseudo RNGs. I suggest you look them up, and try to find one that recommends constant reseeding as a way of improving 'randomness'. > "As I've stated previously, the randomness is in the nonperfect > algorithm in rand(). There is no 'the' algorithm in rand(). There are many different algorithms, and even the simplest ones may differ in the choice of parameters. > But, the set of numbers generated by rand() is > affected by srand(). srand() doesn't affect the randomness of values > that rand() generates, it only changes the set of generated numbers. > Since the algorithm isn't a perfectrandom number generator but a > pseudorandom number generator, the probabilities of certain numbers > occurring is higher than others. These probabilities can be shifted > by calls to srand(). " But not, in general, in a way that makes the resulting sequence exhibit desirable properties such as lack of autocorrelation or other patterns. Consider the simple example: #include <stdio.h> #include <stdlib.h> #include <time.h> void re_seed(int seed) { #ifdef HOSE_ME srand(seed); #endif } int main(int argc, char *argv[]) { int i, count; if ( argc == 1 ) { count = 1000; } else { count = atoi(argv[1]); } srand(time(NULL)); for ( i = 0; i < count; ++i ) { int ri = rand(); re_seed(ri); printf("%d\n", ri); } return 0; } /*EOF*/ Compile this program, examine generated sequences. I generated 10 sequences of 100 numbers with HOSE_ME defined, and 10 without. Then I tested each sequence for autocorrelation. Every sequence with HOSE_ME defined showed statistically significant autocorrelation at alpha = 5% where none of the sequences without HOSE_ME defined did. This is, of course, not a proof, but an example of what we are trying to get across to you. Both versions of the program were compiled with both gcc version 3.4.4 and Microsoft (R) 32bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86. Sinan  A. Sinan Unur <(EMail Removed)> (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/cl...uidelines.html 




A. Sinan Unur 
mensanator@aol.com
Guest
Posts: n/a

Rod Pemberton wrote: > "Keith Thompson" <(EMail Removed)> wrote in message > news:(EMail Removed)... > > "Rod Pemberton" <(EMail Removed)> writes: > > > "A. Sinan Unur" <(EMail Removed)> wrote in message > > > news:Xns9775569186277asu1cornelledu@127.0.0.1... > > >> "Rod Pemberton" <(EMail Removed)> wrote in > > >> news:dtp4td$7tok$(EMail Removed): > > >> > "Keith Thompson" <(EMail Removed)> wrote in message > > >> > news:(EMail Removed)... > > >> ... > > >> >> Calling srand() more than once > > >> >> makes sense *only* if you want to repeat the same sequence. > > >> > > > >> > False. > > >> > > > >> > You apparently meant to say this: "'Calling srand() more than once' > > >> > _with_the_same_value_ 'makes sense *only* if you want to repeat the > > >> > same sequence.'" > > >> > > >> You are misreading Keith's post. Calling srand multiple times with > > >> different seeds during the life time of the program *decreases* the > > >> randomness of the sequence generated. > > > > > > False. > > > > > > What you are claiming is that the randomness of the sequence increases > as > > > the rand() function is used. > > > > No, nobody made that claim. > > > > [snip] > > > > > By calling srand() we increased the > probability of > > > some numbers which had low probability and reduced the probability of > other > > > numbers which had low probability. > > > > So you're asserting that repeatedly calling srand() improves the > > randomness of the numbers returned by rand()? In particular, you're > > claming that that something like this: > <snip> > > is likely to yield better (more random) results than if the second > > call to srand() were removed (perhaps with a different number of > > calls)? > > KEITH: NO! Completely incorrect! This is the fifth time and last time. > Since I'm tied of trying to get through to you, I'll just repeat what I > posted to Sinaur. If you don't comprehend, you can deal with your > inabilities in private. > > "As I've stated previously, the randomness is in the nonperfect algorithm > in rand(). But, the set of numbers generated by rand() is affected by > srand(). No, it's not. There is only ONE sequence. > srand() doesn't affect the randomness of values that rand() > generates, it only changes the set of generated numbers. It does not, as there is only ONE sequence. What srand() does is change your position in the sequence. > Since the > algorithm isn't a perfectrandom number generator but a pseudorandom number > generator, the probabilities of certain numbers occurring is higher than > others. These probabilities can be shifted by calls to srand(). " No, they can't. If, starting at state 123456, and you have the sequence 123456: 123345 123457: 011775 123458: 394875 then that sequence of numbers ALWAYS appears in that order anytime the sequence finds itself at state 123456. If you start at state 123456 and generate 1000 PRNs and you then call srand(), there is a nonzero probability that the state will be set to some number less than 123456. If it were, say 100 less, then your next run of 1000 numbers would have a sequence that duplicates the last 900 numbers of the first block of 1000. Explain how this is "more random". > > > Rod Pemberton 




mensanator@aol.com 


 
Thread Tools  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2)  VK  Javascript  15  05022010 03:43 PM 
Optical Computing: special issue  Natural Computing, Springer  optical supercomputing  C Programming  0  01162009 12:18 PM 
Optical Computing: special issue  Natural Computing, Springer  optical supercomputing  C Programming  0  12192008 12:22 PM 
random.random(), random not defined!?  globalrev  Python  4  04202008 08:12 AM 
Razer Tarantula Gaming Keyboard Review at XYZ Computing at XYZ Computing  Silverstrand  Front Page News  0  11012006 05:13 PM 
Powered by vBulletin®. Copyright ©2000  2014, vBulletin Solutions, Inc..
SEO by vBSEO ©2010, Crawlability, Inc. 