Thread: random isn't really random?

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I found out that case turned out wrong...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    Registered User
    Join Date
    May 2008
    Posts
    81
    Quote Originally Posted by Daved View Post
    >> one would get more random output if srand() is seeded (differently) multiple times.

    What do you mean by more random? What are you measuring that makes you think that your data is more random than it would be if you just used srand() once (or if you just used the mersenne twister prng without rand)?

    As I said earlier, seeding srand every time through the loop should give you identical results as just using the mersenne twister without rand(). That's because if you seed srand and call rand one time you are not getting a random number, you are getting a number that directly corresponds to the value you pass to srand.
    more random meaning less bias = less predictability. for every PRNG, observing a certain number of elements reveals the trend and allows one to predict all subsequent elements. that number is 624 for mersenne MT19937.

    so a 'more' random PRNG would have a higher such number.

    of course, mersenne's output is already 'random' enough for our purposes, but i was wondering if coupling it with rand() would decrease the bias. this was a purely conceptual question. i hear what you are saying about rand() simply spitting out a value based on srand()'s seed, but unless we know how the srand()/rand() algorithm works, i dont think its possible to say whether coupling PRNGs and multiple seeding will or won't decrease bias.

  3. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Look, I think we've already given you an idea on how rand() works.
    Rand takes a number and applies an algorithm on that number to produce varying results from that number. But what is that "number" I'm mentioning? That's what you pass to srand.
    How exactly does rand() work in a in-depths tutorial? I'm afraid I can't answer that either because it's implementation dependant. Every compiler can implement it in a different way.
    Heck, even the above is speculation, because how rand() produces a random number isn't defined--it's up to the designers of the implementation for their compiler to make up how that works, so long as it returns a random number from the seed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Registered User
    Join Date
    May 2008
    Posts
    81
    Quote Originally Posted by iMalc View Post
    Obviously my response wasn't big enough, bold enough or capitalised enough...
    Basically if you don't understand something then you've got to take the advice of those that do understand it...

    If anything, by seeding it more often you make the results LESS random due to the fact that you shorten the period of the PRNG.
    If you properly seed it ONLY ONCE then you will get a properly random sequence of length 2^32 or 2^64 etc before it repeats, depending on how your compiler implements it.
    If you re-seed it then what that does is forgets where it was in that sequence and jumps to another point in the sequence, including potentially the point just before where you were, meaning that you could cause the last few results to be repeated identically again almost immediately which is BAD.

    Btw, there is no such thing as using rand to make something that is more random! The only thing you can improve about it is to even out the distribution over the typical code that simply uses mod with n to give 0 .. n-1. For most practical purposes you will never notice a teensy bit of bias though anyway, that is if the number you mod with happens to even produce any bias to begin with!

    If you have a superrior PRNG such as mersenne then for goodness sakes just use that. If you need cryptographically secure random numbers then you use the Crypto API.
    wow, you sure are passionate about your srand(), lol.

    i don't see how re-seeding it would decrease the period based on what you said. all re-seeding does is resets the period. that doesn't necessarily imply that you are shortening the period. even if you get repetitions, thats ok since repetitions are expected in a truly random sequence. but theres nothing to say that you would get the "last few results to be repeated identically", which is obviously bad.

    anyway, like I said, I call srand() over for each iteration. And in my QRBG thread, I mentioned that I called srand() once for each iteration of a 10,000 iterations loop. By doing so, I was able to get the bell curve for the probability distribution of twin dice tosses, with numerical probabilities extremely close to the predicted. Since I was coupling with the mersenne, I have no doubt that caling srand() just once would have probably yielded similar results. But this is a great indication that calling srand() multiple times doesn't have any adverse effects.

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you listening? Calling srand several times destroys the randomability.
    If you doubt us, then why are you here asking for advice?
    If you don't want our advice or listen to out advice, then why are you here?

    But since you obviously fail to see the difference, here's some statistics for you.
    Here are samples (25 iterations, 1 second delay, srand called on each iteration):
    2406
    2409
    2412
    2416
    2419
    2422
    2426
    2429
    2432
    2435
    2439
    2442
    2445
    2448
    2452
    2455
    2458
    2461
    2465
    2468
    2471
    2475
    2478
    2481
    2484

    Here are samples (25 iterations, 0 second delay, srand called once):
    2181
    5570
    7297
    29776
    625
    19023
    17188
    12741
    15153
    15603
    17361
    6201
    30635
    15432
    6854
    15035
    10045
    16518
    19564
    1253
    23726
    20648
    17895
    26905
    21349

    Tell me which one is more random.
    Last edited by Elysia; 06-08-2008 at 01:46 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Registered User
    Join Date
    May 2008
    Posts
    81
    Quote Originally Posted by Elysia View Post
    Are you listening? Calling srand several times destroys the randomability.
    If you doubt us, then why are you here asking for advice?
    If you don't want our advice or listen to out advice, then why are you here?
    calm down. i'm here for the same reason you are supposedly here, to learn. please read my last post. i called srand() 10,000 times in a single program and it did nothing to "destroy the randomability". that pretty much kills your argument.

  7. #37
    Registered User
    Join Date
    May 2008
    Posts
    81
    Quote Originally Posted by Elysia View Post
    Are you listening? Calling srand several times destroys the randomability.
    If you doubt us, then why are you here asking for advice?
    If you don't want our advice or listen to out advice, then why are you here?

    But since you obviously fail to see the difference, here's some statistics for you.
    Here are samples (25 iterations, 1 second delay, srand called on each iteration):
    2406
    2409
    2412
    2416
    2419
    2422
    2426
    2429
    2432
    2435
    2439
    2442
    2445
    2448
    2452
    2455
    2458
    2461
    2465
    2468
    2471
    2475
    2478
    2481
    2484

    Here are samples (25 iterations, 0 second delay, srand called once):
    2181
    5570
    7297
    29776
    625
    19023
    17188
    12741
    15153
    15603
    17361
    6201
    30635
    15432
    6854
    15035
    10045
    16518
    19564
    1253
    23726
    20648
    17895
    26905
    21349

    Tell me which one is more random.
    thats interesting. how were you seeding srand()? Was it the same value every time? If so, then that explains why you're seeing a less random distribution in the first case.

  8. #38
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i called srand() 10,000 times in a single program and it did nothing to "destroy the randomability". that pretty much kills your argument.
    But your argument would be that calling it many times improves the randomness, and it doesn't.

    Because there is no way to know the implementation of the standard rand() / srand() functions, it's impossible to say what the effect of calling srand() many times with very similar seed values (say incrementing time) would be. Sure you may see nothing untoward, but other people (with other implementations) would see something far different.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First test scenario:
    Code:
    int main()
    {
    	srand( time(NULL) );
    	for (int i = 0; i < 25; i++)
    		cout << rand() << endl;
    }
    Second test scenario:
    Code:
    int main()
    {
    	for (int i = 0; i < 25; i++)
    	{
    		srand( time(NULL) );
    		cout << rand() << endl;
    		Sleep(1000);
    	}
    }
    And for the sake of it, if I remove the sleep and call srand inside the loop, I get this:

    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    2285
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Elysia, shawnt was seeding srand with a random number generated from a Mersenne Twister PRNG. So with a random seed, the output of rand() will be much more random than with your example.

    I still don't see how combining the two helps in any way, but I'm not sure shawnt does either, he's just hoping.

  11. #41
    Registered User
    Join Date
    May 2008
    Posts
    81
    Quote Originally Posted by Daved View Post
    Elysia, shawnt was seeding srand with a random number generated from a Mersenne Twister PRNG. So with a random seed, the output of rand() will be much more random than with your example.

    I still don't see how combining the two helps in any way, but I'm not sure shawnt does either, he's just hoping.
    rite. thanks

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    True, but it shows the point that re-seeding srand does not help in producing more random numbers. This is the best I can do since I don't have the PRNG.
    I would note that rand echoes the exact same number when seeded with a new number. It's not only until the second time that it starts to differentiate.

    I'm not sure it's possible to put any more evidence into this.
    Use one. Don't re-initialize srand all the time. It wasn't designed to work that way and may not provide any results different from just getting that random number from PRNG.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Registered User
    Join Date
    May 2008
    Posts
    81
    Quote Originally Posted by Salem View Post
    > i called srand() 10,000 times in a single program and it did nothing to "destroy the randomability". that pretty much kills your argument.
    But your argument would be that calling it many times improves the randomness, and it doesn't.

    Because there is no way to know the implementation of the standard rand() / srand() functions, it's impossible to say what the effect of calling srand() many times with very similar seed values (say incrementing time) would be. Sure you may see nothing untoward, but other people (with other implementations) would see something far different.
    daved said it above, but just to clarify, my argument was the calling rand() multiple times wouldn't be counter-productive, NOT that it improves randomness. the latter was just a shot in the dark, which I said so myself.

    I also said that its precisely because we do not know how srand() is implemented, that we cannot say that it will improve OR worsen randomness.

    lastly, the choice of seed is imo all important. in my experience, time(NULL) is only effective after a wait of 1000 ms, and even so produces linear results.

    here's what I got my re-seeding srand() everytime with the output of the MT19937 prng:

    as you can see, those results indicate a remarkably efficient randomness.

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by shawnt View Post
    in my experience, time(NULL) is only effective after a wait of 1000 ms,
    That is because you are NOT supposed to re-seed. You are supposed to do it ONCE and ONCE only, and from thereafter ONLY call rand and you will get your random numbers.

    and even so produces linear results.
    Then blame rand() and not srand(). srand() ONLY gives rand() an initial number to work with. I think I proved that to you before.
    If it produces linear results, it's because the rand() algorithm is not sophisticated enough for you in which case re-seeding multiple times will NOT help.
    You should use a DIFFERENT random number generating library instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by shawnt View Post
    I also said that its precisely because we do not know how srand() is implemented, that we cannot say that it will improve OR worsen randomness.
    Oh so that's why we're being ignored, you're assuming we haven't seen the exact implementation of rand used by numerous compilers. Well it just so happens I am familiar with some of them.
    lastly, the choice of seed is imo all important. in my experience, time(NULL) is only effective after a wait of 1000 ms, and even so produces linear results.
    Again, this is nonsense. Since you're never supposed to seed it between each random number then that coment is wrong.

    You also said that by your interpretation of my post you didn't understand how you could cause it to repeat the last few results again. That's fine, just know that you're the one in the dark there, as some of us do know that you can cause this to occur. The things is though, that the way you're wrongly using it, even if you do make it start repeating part of the sequence, then you'll jump again to a different point before you repeat very much. That still doesn't make it better though!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM