Public Group

# My rand() doesn't work

This topic is 4114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
You'll need to generate a seed value for rand() otherwise it will generate the same numbers each time you run your game.

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 on other sites
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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by Adam4444In 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 on other sites
I think your existing code will work if you just seed the randomizer, as others have said.

##### Share on other sites
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.

• 11
• 18
• 12
• 22
• 9
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999822
×