I realize this probably isn’t an SDL issue, per se (though I am doing a
"srand(SDL_GetTicks());"), but I just tried the Mac OS X port of my game
"Gem Drop X" [ http://www.newbreedsoftware.com/gemdropx/ ],
and discovered that the game is UNPLAYABLY bad. Not random at all! :^(
Anyone know if there’s some trick to random seeding/generation on OSX?
Regardless of what the seed is, there’s definitely too much of a pattern
in the results. (Unless a very low srand() value causes less random
results from rand() !?)
///--------------------------------------------------------------------------------------
// GetRandom - generates pseudo random number. min and max are inclusive
///--------------------------------------------------------------------------------------
int GetRandom( int min, int max )
{
int num;
float random;
random = rand()/(RAND_MAX + 1.0); //a number from 0.0 to 0.9999
num = (int)((1+ max-min) * random) ;
num += min;
return num;
}
This code generates great random numbers, and is pure ANSI.
Do you really want to do that? If you do that at the beginning of
your program, you might well always be getting zero. (I think I did
that once, too.) srand(time(NULL)); is probably what you want.
(I suppose it’s possible that sending a zero to srand() might break the
results, though that’d certainly be a bug.)On Thu, Sep 11, 2003 at 07:51:15PM -0700, Bill Kendrick wrote:
I realize this probably isn’t an SDL issue, per se (though I am doing a
“srand(SDL_GetTicks());”), but I just tried the Mac OS X port of my game
I realize this probably isn’t an SDL issue, per se (though I am doing a
“srand(SDL_GetTicks());”), but I just tried the Mac OS X port of my game
“Gem Drop X” [ Gem Drop X [New Breed Software] ],
and discovered that the game is UNPLAYABLY bad. Not random at all! :^(
Anyone know if there’s some trick to random seeding/generation on OSX?
I think back when I first learned of SDL_GetTicks(), I thought it
gave realtime, not process time (therefore returning ‘0’ at the
beginning of execution )
I’ll update my code, and see if I can find someone to rebuild
Gem Drop X for OSX.
Man, I REALLY need my own OSX box. :^(
-bill!On Thu, Sep 11, 2003 at 11:02:03PM -0700, MC wrote:
Ok, I know I’m being pedantic, overly nit-picky, and all that, but once
again, just for the record, there is no such thing as a random number, only
random sequences. And while I’m being a royal pain, I’d also like to point
out that “infinite number” is an oxymoron.
Cheers
JOn Thursday 11 September 2003 11:02 pm, MC wrote:
This code generates great random numbers, and is pure ANSI.
When you play it, you should notice that the gems that are ‘dealt’ to you
end up in a pattern kind of like:On Thu, Sep 11, 2003 at 11:40:21PM -0700, MC wrote:
Hey bill, I got a G4 laptop with project builder right next to me
If you wan’t me to build it for you, I wouldn’t mind. Just being a good
Cyber-sameritan or somethin.
+++++++*
+++++++*
+++++++*
Try changing my “srand()” call to use “time(NULL)” instead of
“SDL_GetTicks()”
I’m beginning to wonder, even though rand() stuff is ANSI standard,
if it might be good to have some random ‘helper’ functions for us
dumb-dumb heads who never thought to use time() instead of SDL_GetTicks()
e.g.:
SDL_SeedRandom(); /* it does it based on time(), or equivalent, itself */
…
my_variable = SDL_Random(10); /* returns a number between 0 and 9, say */
From the OS X srand() page:On Friday, September 12, 2003, at 03:03 PM, sdl-request at libsdl.org wrote:
Date: Thu, 11 Sep 2003 19:51:15 -0700
From: Bill Kendrick
To: SDL
Subject: [SDL] Randomness issues in OSX
Reply-To: sdl at libsdl.org
rand, srand, sranddev, rand_r - bad random number generator
…
These interfaces are obsoleted by random(3).
Note that the Linux srand() implementation actually uses the much
better srandom()/random() algorithm, which probably explains the
discrepancy between OS X and Linux.
I don’t know how standard srandom()/random() is, but it is part of the
C library on both Linux and Mac OS X (it seems random() came from
4.2BSD originally). random() is apparently much slower than rand()
however.
You could also write your own random number generator - it’s easy to
write a decent one using the linear congruent method, and it will
probably be much better than the old srand() function.
It’s easy to write a bad random generator using the linear congruential
method. Writing a good one isn’t trivial. The values chosen for the
modulus, multiplier and increment are critical. See Donald Knuth’s “The Art
of Computer Programming”, Volume 2, chapter 3 for a good analysis of LCM.
Unless you understand the mathematical analysis, and are prepared to invest a
huge amount of time in running statistical tests, you’re probably better off
just using the random() in your compiler’s library.
“It’s difficult to be sure that a particular generator is good without
investing an enormous amount of effort in the various statistical tests.”
-Robert Sedgewick, “Algorithms in C”
JOn Friday 12 September 2003 01:11 pm, Darrell Walisser wrote:
You could also write your own random number generator - it’s easy to
write a decent one using the linear congruent method,