Sign in to follow this  
mello

Negative and Float random numbers

Recommended Posts

Hello community, Oh... today I've a really huge problem! I've a function called draw_forest() with the following prototype:
void draw_forest(GLubyte color[], int color_variation, float max_height, float min_height);
The function calls another function to draw several trees in the screen with a color variation and with the height between min_height and max_height. If color[]={100, 100, 100} (colors in the 0-255 range) and color_variation=30 then the tree should be drawn with color components in the range 70-130. Well, for the height I've this code:
float height_range = height_max - height_min;
float height_temp = height_min + (rand()%height_range);
This solution doesn't work because rand() doesn't accept floats. rand() also doesn't return negative numbers so I'm having trouble with the colors, too. Does anyone have some tips on how to solve this? Thanks a lot!

Share this post


Link to post
Share on other sites
Hi,

I've created a set of functions just for this purpose. (I'm assuming you're using C++)
float random(){
return (float)rand() / (float)RAND_MAX;
}

float random(float max){
return (float)rand()*max / (float)RAND_MAX;
}

float random(float min, float max){
return random() * (max-min) + min;
}

int randomi(int max){
return rand() % max;
}

int randomi(int min, int max){
return rand() % (max-min) + min;
}


As you can see, randomi (overloaded), accepts a min and max value which can be negative. The random() function also has min and max capabilities for floating point numbers.

Hope those are helpful
~zix

Share this post


Link to post
Share on other sites
Quote:
Original post by mello
his solution doesn't work because rand() doesn't accept floats.


No, it's the % operator that doesn't accept floats. Convert height_range to an int ( (rand()%(int)height_range) ) or use the function fmod from the cmath header.

Quote:
Original post by mello
rand() also doesn't return negative numbers so I'm having trouble with the colors, too.


If colors have to be between 0 and 255 why do you need negative numbers? Anyway just rand a higher number a subtract a certain amount. Examples:

-127 to 128 = ( rand() % 256 ) - 127

-3 to 9 = ( rand % 13 ) - 3

or

(where A is negative and B is positive )
A to B = ( rand() % ( ( B - A ) + 1 ) ) + A

Share this post


Link to post
Share on other sites



int randomi(int min, int max){
return rand() % (max-min) + min;
}




There is one fundamental problem with the code above. A singular number produced by the code is random and it is in between the min and max ranges.
The problem however is that eventually the code produces more random numbers near the minimum value (unless, RAND_MAX is divisible by max-min).

To prove this, you can create like 10000 random numbers and check which numbers occure more often.

Cheers !

Share this post


Link to post
Share on other sites
Yay... i know this one... here is my solution to random numbers

i use two overloaded functions "rand(int,int)" and "rand(float,float)"


// INT rand(x,y)
int rand(int min, int max) // Generates random INT between X and Y
{ //
float r = rand(); // Generate random number
float i; // number to hold scaled random number
i = (r/RAND_MAX)*(max-min)+min; // Scale the random number to 0-1 then multiply it by the new range, and add the offset
return i; // Return the scaled random number
} //


// FLOAT rand(x,y)
float rand(float min, float max) // Generates random INT between X and Y
{ //
float r = rand(); // Generate random number
float i; // number to hold scaled random number
i = (r/RAND_MAX)*(max-min)+min; // Scale the random number to 0-1 then multiply it by the new range, and add the offset
return i; // Return the scaled random number
}


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