Sign in to follow this  
Mania-92

My rand() doesn't work

Recommended Posts

Mania-92    122
I am maken a little snake game with SDL, but when I wan't to spawn te food it always spawns at the same point, so what's wrong with my rand()?
box.x = (double)rand() / (RAND_MAX + 1) * (640 - 0)
            + 0;
box.y = (double)rand() / (RAND_MAX + 1) * (480 - 0)
            + 0;

Share this post


Link to post
Share on other sites
Rand    193
To see whats going wrong try printing out loads of rand() in a for loop just as a test, to check that first. Its also good practice to you brackets around things to make sure the order of things. Whats RAND_MAX? Is it an int or float? Whats the same point these are coming out at? Is it 0?

Share this post


Link to post
Share on other sites
Naku    151
You'll need to generate a seed value for rand() otherwise it will generate the same numbers each time you run your game.

Try adding :

srand ( time(NULL) );

during your initialisation. This will use a different seed value each time you run based on the current time.

You'll also need to add Time.h to your includes if you aren't already in order to use the time( ) function.

Share this post


Link to post
Share on other sites
Mania-92    122
Not it's not 0, something around 400 (x) 100 (y)

EDIT: Uhm anyone a example of a snake game spawning food?

[Edited by - Mania-92 on March 17, 2007 8:48:12 AM]

Share this post


Link to post
Share on other sites
Adam4444    100
In my snake game I used this function for generating the positions for the food:
void place_food(void)
{
do {
fx = nrand(1, WIDTH - 1);
fy = nrand(1, HEIGHT - 1);
}
while (obj(fx, fy) == B_SNAKE);
}


It keep generate values till the positions is empty (i.e the snake isn't occupying them).

And the nrand() function:
int nrand(int lower, int upper)
{
int diff = upper - lower + 1;
int a = rand() % diff + lower;

return a;
}


Then in my "graphic function", which generates all the graphic, I just wrote the food graphic at (fx, fy).

Share this post


Link to post
Share on other sites
Crypter    748
random numbers is based off a number that must be seeded when the program
starts. If this number is always the same (never reseeded), the same number
will yeld as a resault.

In addition to the previous posters, insure you seed the random number
generator at the beginning of your program:

srand ( time(NULL) );

Share this post


Link to post
Share on other sites
Mania-92    122
Quote:
Original post by Adam4444
In my snake game I used this function for generating the positions for the food:*** Source Snippet Removed ***

It keep generate values till the positions is empty (i.e the snake isn't occupying them).

And the nrand() function:*** Source Snippet Removed ***

Then in my "graphic function", which generates all the graphic, I just wrote the food graphic at (fx, fy).

Could I use it?

Share this post


Link to post
Share on other sites
iMalc    2466
If all those zeros that you add or subtract are there so that you can change how close the food is spawned to the edge, then you should make them into one or more constants. Otherwise, it makes sense to delete the zeros.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this