How to Random blit?

Hello all,

I’m working on my project.I’m having problem’s picking
a random number from 1 to 3. If i could just get this
straight, my game could move on. Any help is
appreiciated! Here’s the function:

void which_scen()
{
srand(what goes here);
ran_num = (rand() % 3);

if (ran_num == 0) {
scen_1(); }
if (ran_num == 1) {
scen_2(); }
if (ran_num == 2) {
scen_3(); }
}

Thanks__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.

srand(what goes here);

Try:

srand(SDL_GetTicks());

Only do it once in your program, though… no point in seeding rand
continuously.

-bill!

GEM Products wrote:

Hello all,

I’m working on my project.I’m having problem’s picking
a random number from 1 to 3. If i could just get this
straight, my game could move on. Any help is
appreiciated! Here’s the function:

void which_scen()
{
srand(what goes here);
ran_num = (rand() % 3);

if (ran_num == 0) {
scen_1(); }
if (ran_num == 1) {
scen_2(); }
if (ran_num == 2) {
scen_3(); }
}

First of all, only seed (srand) once, at the beginning of your program,
with the current time (or some other suitably random value). A
pseudorandom sequence is deterministic, so given the same seed, you’ll
have the same sequence. If you always seed, you’ll always get the same
random number.

Second, most pseudorandom number generators use a linear congruential
sequence, in which the lower bits are suspect. Therefore, do not use
modulus (%) to compute a random number in a range.

Instead, use floating point arithmetic to divide by RAND_MAX to get a
random number between 0.0 and 1.0. Then, scale and translate that number
to fit your range. Be careful because it’s a half-open range! Also be
careful casting back to an int.

E.g. to get a random number between 0 and n-1, use

(double(rand()) / RAND_MAX) * n

Finally, I’d really consider reindenting your code. That style is really
painful to read and doesn’t correspond to the two most prevalent styles.
I’d recommend using else-if to underscore the fact that only one clause
will execute, and reduce the unnecessary checking.–
Marc A. Lepage
Software Developer
Molecular Mining Corporation
http://www.molecularmining.com/

Hello all,

I’m working on my project.I’m having problem’s picking
a random number from 1 to 3. If i could just get this
straight, my game could move on. Any help is
appreiciated! Here’s the function:

void which_scen()
{
srand(what goes here);
ran_num = (rand() % 3);
/* According to the man page it is very preferable to do

  • something like: /
    ran_num = 1+ (int)((3.0
    rand())/(RAND_MAX+1.0))

if (ran_num == 0) {
scen_1(); }
if (ran_num == 1) {
scen_2(); }
if (ran_num == 2) {
scen_3(); }
}

Thanks

The argument to srand is called a “seed”, which is used to
initialize the random number generator. The seed should
be as random as possible. I would normally recommend
something such as how many milliseconds since the computer
booted, or ambiant noise off the joystick port. SDL doesn’t
seem to support either of these. It does support
SDL_GetTicks. I do not recommend this as a particularly
random number, because it only tells the number of ms since
SDL_Init was called. If there is no user input between when
SDL_Init is called and when srand is called, there is likely
very little variation in this value.

I did want to warn you that using a technique like this
(depending on how you apply it) can cause shimmering,
which can be asthetically un-pleasing. I recommend one
of the two following as alternatives… they both require
selecting an anchor to prevent shimmering. Unless you
plan on dragging this image, surface coordinates often
provide a good anchor:

I recommend either generating a random number, and storing
it in an array indexed by your anchor… and at blit-time
pull the value from the array.

It may also be a good idea to use a simple fractal function
that uses the anchor as coordinates into the fractal to
generate pseudo-random patterns that are for all intents
and purposes fixed.

Best of luck,

-Loren


Great news! Get free KNXmail here!
http://www.knx1070.com

srand(what goes here);

Try:

srand(SDL_GetTicks());

As was pointed out, SDL_GetTicks() at the beginning of your program isn’t
very random. Using time(NULL) is usually a good substitute.

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

As was pointed out, SDL_GetTicks() at the beginning of your program isn’t
very random. Using time(NULL) is usually a good substitute.

I typically do it after loading graphics and such, so the amount of time
that passes between running the program one time and running it again another
will depend a lot on how long it took to load graphics (which can be
interrupted by other processes, affected by drive head seek time, etc.)

Is “time(NULL)” truly cross-platform? I’d like to think so, but for
some reason I have a feeling it isn’t.

Perhaps we need an “SDL_GetTicksSinceBoot()” function? :slight_smile:

-bill!

Is “time(NULL)” truly cross-platform? I’d like to think so, but for
some reason I have a feeling it isn’t.

I think it is fairly portable.

Perhaps we need an “SDL_GetTicksSinceBoot()” function? :slight_smile:

NO! :slight_smile:
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

From the time manpage:

CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, BSD 4.3

That covers quite a few platforms…

–Ryan

Sam Lantinga wrote:>

Is “time(NULL)” truly cross-platform? I’d like to think so, but for
some reason I have a feeling it isn’t.

I think it is fairly portable.

Perhaps we need an “SDL_GetTicksSinceBoot()” function? :slight_smile:

NO! :slight_smile:
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

srand(SDL_GetTicks());

Not if SDL_GetTicks starts counting from SDL initialization, as documented,
unless you do it after an interactive event.

The traditional unix way is srand(getpid() ^ time(NULL)) or some variation
thereof. #ifndef unix #define getpid() 1, and it’s almost portable :slight_smile:

– Mattias

I’ve just wrote a:
SDL_MailSamWithInterestingFunctionIdea(char *function_name);
I’ll send a patch…On Fri, 12 May 2000, Sam Lantinga wrote:

Is “time(NULL)” truly cross-platform? I’d like to think so, but for
some reason I have a feeling it isn’t.

I think it is fairly portable.

Perhaps we need an “SDL_GetTicksSinceBoot()” function? :slight_smile:

NO! :slight_smile:
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

Martin

Bother," said the Borg, “we assimilated a Pooh.”