Public Group

# srand

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

## Recommended Posts

Hello I am trying to generate random numbers but my random number generator doesnt seem to be working properly, I believe it has something to do with the setting of srand. The random number generating function belongs to a class so I am not sure where to place the srand for it to work properly. Thanks
//This function gets called only once from winmain, runs the rest of the program
void Game::run()
{
//Seed random number
DWORD time = timeGetTime();
srand(time);

//Loop
main_loop();
}

//Random number generating function
double Game::random_number(int range_min, int range_max)
{
return ((double)rand()/RAND_MAX + 1) * (range_max - range_min) + range_min;
}

/*The function that uses the random number generating function (Even though it work it's awkward about it, all meteors seem to get rotated in same direction*/
void Asteroid::update(Game &game)
{
rotate((float)game.random_number(-1,1));
}



##### Share on other sites
Quote:
 Original post by AntonymHello I am trying to generate random numbers but my random number generator doesnt seem to be working properly, I believe it has something to do with the setting of srand. The random number generating function belongs to a class so I am not sure where to place the srand for it to work properly.
Define "not working properly". That code looks fine to me - the main thing is that you want to make sure to only call srand() once in your application.

##### Share on other sites
srand is generally placed at the initialization of your program (at the beginning of main() or WinMain for example).

Other than that I can't see anything wrong with that code, although I usually do
srand(time(NULL));

Be sure to include ctime if you're going to use that piece of code.

##### Share on other sites
It only rotates in one direction it seems.

##### Share on other sites
You have an operator precedence problem. Try:
((double)rand()/(RAND_MAX + 1))

##### Share on other sites
I tried the last suggestion, but nothing changed. I took that code from the one of the library examples.

//The part of code that calls the update function on every object, also moves //objects	it = game_objects.begin();	for(; it != game_objects.end(); it++)	{		(*it)->update(*this);		(*it)->move();		if((*it)->get_hit_animation())		{			(*it)->play_hit(*this);		}	}//Rotationvoid Object::rotate(float theta){	rotation -= theta;	if(rotation < 0) rotation += 360;	if(rotation > 360) rotation -= 360;	update_velocity();}//Velocityvoid Object::update_velocity(){	velocity.x = speed * sin(degrees_to_radians(rotation));	velocity.y = speed * cos(degrees_to_radians(rotation));}//Movevoid Object::move(){	position.x += velocity.x;	position.y -= velocity.y;}

##### Share on other sites
Maybe convert (RAND_MAX+1) to float/double before dividing by it. I remember having problems with that some years ago, since then I always cstyle-cast or static_cast (RAND_MAX+xyz) to float/double.

A rule of thumb I personally follow: Always explicitly match the type and don't rely on implicit typecasts.

##### Share on other sites
Quote:
 Exactly, C++ always operates from right to left. So if you want to cast a variable to another type you have to do it at the rightmost operand.

I have never heard such rule (C++ mostly operates in undefined order), and as far as I know int / double => double as well as double / int => double.

#include <iostream>int main(){    int n = 2;    double d = 3.5;    std::cout << n + d << ' ' << d + n << '\n'; //outputs: 5.5 5.5}

What if RAND_MAX + 1 overflows int (but then you should get a divide by zero). Perhaps try RAND_MAX + 1.0

//Rotationvoid Object::rotate(float theta){	rotation -= theta;	if(rotation < 0) rotation += 360;	if(rotation > 360) rotation -= 360;	update_velocity();}

Wouldn't passing a different small value to it all the time make it slightly turn one way and another (and then perhaps do something drastic if it goes under 0 or over 360). It looks more like it would expect the same value every time it is called to make the object rotate in one or another direction at constant speed, or reducing or increasing theta over time to make the object rotate slower or faster.

##### Share on other sites
Quote:
 Original post by HomerSpExactly, C++ always operates from right to left.

You have no idea what you're talking about. C++ has these things called "operator precedence" and "associativity". Operator precedence means that the expression a + b * c has the same meaning as b * c + a. Some operators are right to left associative, like the assignment operators so a = b = c is always the same as a = (b = c). But some operators are left to right associative like the arithmetic operators so a - b - c is the same as (a - b) - c and never a - (b - c).

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by HomerSpExactly, C++ always operates from right to left.

You have no idea what you're talking about. C++ has these things called "operator precedence" and "associativity". Operator precedence means that the expression a + b * c has the same meaning as b * c + a. Some operators are right to left associative, like the assignment operators so a = b = c is always the same as a = (b = c). But some operators are left to right associative like the arithmetic operators so a - b - c is the same as (a - b) - c and never a - (b - c).

You are of course right, I wasn't thinking straight.
Post deleted.

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631724
• Total Posts
3001903
×