Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GothicChess.Com

macrumors regular
Original poster
Apr 6, 2007
126
0
Philadelphia, PA
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Code:
    srand((unsigned char)time(NULL));

has worked for me in the past.

I then use

Code:
random()

to get a random number.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 

GothicChess.Com

macrumors regular
Original poster
Apr 6, 2007
126
0
Philadelphia, PA
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?


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
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
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().
 

ChrisA

macrumors G5
Jan 5, 2006
12,907
2,156
Redondo Beach, California
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.
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
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...)
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
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...
 

Thinine

macrumors newbie
Jul 23, 2002
24
0
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.
 

GothicChess.Com

macrumors regular
Original poster
Apr 6, 2007
126
0
Philadelphia, PA
Good catch!

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().

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 :) :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.