Random Number Seeding

Discussion in 'Mac Programming' started by GothicChess.Com, Jun 17, 2007.

  1. GothicChess.Com macrumors regular

    GothicChess.Com

    Joined:
    Apr 6, 2007
    Location:
    Philadelphia, PA
    #1
    OK, this question is so simple it's embarrassing.

    I used to call GetDateTime(&RandSeed) way back in OS 9 to randomize the seed that generates random numbers.

    I have no idea how to do this in Carbon now using XCode in OS X.

    I tried srandom(time(NULL)); to no avail, and srandomdev(); also. No matter what I do, I get the same sequence of random numbers when I launch the program.

    Anyone know how to do this in C?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Code:
        srand((unsigned char)time(NULL));
    
    has worked for me in the past.

    I then use

    Code:
    random()
    
    to get a random number.
     
  3. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    Code:
    srandom(time(NULL));
    
    int i;
    for (i = 0; i < 10; i++) {
        printf("%lu\n", random());
    }
    Dunno, this should definitely work. On my machine this appears to reliably produce a totally different sequence each time it is run. Maybe you can post the code snippet you're using to produce and format/verify the random numbers?

    Sample output:
    Code:
    [Session started at 2007-06-17 03:02:32 -0700.]
    2090941647
    1713717935
    19545553
    493645602
    1103586891
    5362759
    1599638795
    441909137
    453567014
    943252383
    
    [Session started at 2007-06-17 03:06:36 -0700.]
    1973348459
    192630836
    766767582
    1797493473
    898240775
    1902593298
    719372099
    1488685032
    1621393682
    1290816216
    
    [Session started at 2007-06-17 03:06:46 -0700.]
    1063765015
    1528177334
    1877317850
    62974090
    532820706
    178201439
    933498216
    281588473
    636726690
    2008441084
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    I use srand((unsigned)time(NULL)); to seed and then rand(); to get the random number, this is in Cocoa but seems to work OK.
     
  5. GothicChess.Com thread starter macrumors regular

    GothicChess.Com

    Joined:
    Apr 6, 2007
    Location:
    Philadelphia, PA
    #5

    Yeah I thought it should also.

    I use the random numbers to select an opening move from my checkers program's opening book. I have several good moves available per position, but it keeps playing the same move every time! So, on startup, I have it print out a random number from 1 to 1000 and add it to my log file

    Code:
    Sunday, June 17, 2007 @ 3:44:53 AM
    Opening book: 3000017 positions reserved requiring 48000272 bytes, with 220 actual positions entered.
    
    random number test = 249
    
    Sunday, June 17, 2007 @ 3:47:04 AM
    Opening book: 3000017 positions reserved requiring 48000272 bytes, with 220 actual positions entered.
    
    random number test = 249
    
    Sunday, June 17, 2007 @ 4:07:52 AM
    Opening book: 3000017 positions reserved requiring 48000272 bytes, with 220 actual positions entered.
    
    random number test = 249
    
    Sunday, June 17, 2007 @ 4:09:14 AM
    Opening book: 3000017 positions reserved requiring 48000272 bytes, with 220 actual positions entered.
    
    random number test = 249
    
    And here is the actual code that generates the random number:

    Code:
    srandom(time(NULL));
    fprintf(g_report_info, "random number test = %d\n\n", abs(rand()) % 1000);
    
    The funny thing is, I can solve large endgame databases so the checkers program can announce a win in 253 ply, but I can't get a random seed to work!

    :)

    The program, when finished, will be made available here:

    http://www.onlyperfectcheckers.com
     
  6. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #6
    If you are using rand(), you must seed it with srand() or sranddev().

    I would however recommend using random(), as it's better.

    To clarify:
    rand() is seeded by srand() or sranddev().
    random() is seeded by srandom() or srandomdev().
     
  7. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    I never use the time() function as a seed. It's value changes only once per second s it make a poor andom seed. I make a call to gettimeofday() then extract tv_usec from the tv structure and use that. If the program accepts any user input I will get the tv_usec value twice, once before and once after accepting user input and use the difference as the seed.
     
  8. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #8
    You're right that using time() as a seed isn't the best option, but why not just use srandomdev()? It will give you an extremely good seed with no fuss.

    IMO the only reason for needing srandom() is if you need to set the seed to a particular value, such as one stored from a previous session. Some games do this to stop the player cheating by returning to a saved game if a "random" outcome goes against them. (We've all done it, haven't we...)
     
  9. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #9
    Having said that, I'm not sure if srandomdev() is available on all platforms, so it might be necessary to use srandom() if you're not writing for Mac OS X only...
     
  10. Thinine macrumors newbie

    Joined:
    Jul 23, 2002
    #10
    Are you looping that part of your program? If you repeated seed the generator with the same value, you're going to get the same value out.
     
  11. GothicChess.Com thread starter macrumors regular

    GothicChess.Com

    Joined:
    Apr 6, 2007
    Location:
    Philadelphia, PA
    #11
    Good catch!

    Ahhh, this is it, indeed! Nice catch! I had completely forgotten about this in my 3 AM coding effort. Changing rand() to random() has alleviated the problem! Nicely done!

    To answer some of the other questions posed by others:

    I am not seeding in a loop, just seeding once on startup. The number of random choices available is rather small, as I have only 3 possible "best" moves (maximum) per checkers position in the opening book. But, after playing the move 11-15, the program was always responding 22-18 (and never 22-17 nor 23-19, the other 2 moves in the book.)

    This has now been cured.

    Excellent :) :)
     

Share This Page