Generating a Random Number?

I need code to generate a random number between -95 and 95. The code I'm currently using is seeded with the time, so when I call it over and over again rapidly, my cube (part of a game I'm working on) goes left to right, then starts over. Could someone please provide me with code that reliably creates a random number between -95 and 95? Thanks.

My Code:
1
2
3
4
5
void movecube(){
	srand ( time(NULL) );
	e = rand() % 95 + -95;
	f = rand() % 95 + -95;
}
rand() % 190 - 95;
I think that should do it
rand() % 191 - 95; ;)
1. if you can, move "srand ( time(NULL) );" to a higher block level where you enter the game, or reset the game.
2. rand() % 95, strictly speaking, is not a uniform random number, because 95 is not a divisor of the RAND_MAX. So you end up with a slight bias which is usually negligible.
3. for a portable rand generator, you will need to code on your own.
everid wrote:
if you can, move "srand ( time(NULL) );" to a higher block level where you enter the game, or reset the game.

Not if he can. That's what he must do. Randomize using srand only once in your program
(usually first thing inside main).

everid wrote:
rand() % 95, strictly speaking, is not a uniform random number, because 95 is not a divisor of the RAND_MAX. So you end up with a slight bias which is usually negligible.

Also correct. Check here for a way to fix this:
http://cplusplus.com/forum/lounge/23263/page7.html#msg126360
Topic archived. No new replies allowed.