# Random number inside bounds

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

## Recommended Posts

It should be pretty straight forward, however it just wont work as i want it to, every time its used i get x = 0 and z = 0 instead of having them inside the bounds i provide the function. (im trying to spawn trees randomly around terrain) Here is my code;
SF_INLINE void GetRandomVegVec(D3DXVECTOR3& out, int boundsX, int boundsZ)
{
int rand_x = rand() % boundsX + 1;
int lol_x = rand() % boundsX + 1;
int rand_z = rand() % boundsZ + 1;
int lol_z = rand() % boundsZ + 1;
out.x = (float)(rand_x - lol_x);
out.z = (float)(rand_z - lol_z);

// Project onto unit sphere.
D3DXVec3Normalize(&out, &out);
}



##### Share on other sites

out.x = static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * static_cast<float>(boundsX);out.z = static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * static_cast<float>(boundsZ);

##### Share on other sites
It did not work either, but i found out by removing the D3DXVec3Normalize it stopped giving only 0,0 values, but instead after a few spawns it would crash with the error

"Vector subscribt is out of range"

##### Share on other sites
Solved it, thanks anyway! :D

##### Share on other sites
Subtracting two evenly-distributed random numbers produces a triangular distribution. Is this what you intented?
Are you aware that all those +1's are cancelling each other out due to the subtractions?

If your goal is to get a random vector of unit length pointing in a random direction with each direction being equally probable, then you'll need to take another approach.
One method is generating the vector using polar coordinates, which is pretty straightforward in 2D. You just generate a random angle and convert to cartesian coordinates.
Another method is the rejection method where you generate points within a 2x2 square and reject those outside an encolsed cirle that touches all four walls and is of radius 1, repeating the loop until you get a position that is not rejected, and then you scale the resulting vector to the right length.

##### Share on other sites
Just use boost.random.
It will provide strong randomization with the kind of the distribution you want, and with the bounds you want, all of that correctly.

##### Share on other sites
@ iMalc - I think you overestimated what i tried to achieve :P, i simply just wanted a random number between two bounds, in my case those bounds would be -512,512 so anything between those would be acceptable.

I found out that i had been multiplying by the wrong values instead of the bounds which caused my GetHeight(x,z) function to get out of bounds >.>

##### Share on other sites
Quote:
 Original post by Lnf1nity@ iMalc - I think you overestimated what i tried to achieve :P, i simply just wanted a random number between two bounds, in my case those bounds would be -512,512 so anything between those would be acceptable.
Hmm, that seems consistent with your initial comment as well but the D3DXVec3Normalize, and the associated comment really suggested that you were trying to do something else. I usually trust what the code shows me about what a person is trying to do more than their own description. But I guess even that is going to be wrong sometimes.
Assuming you do want an even distribution, did you arrive at a solution similiar to "rand() % 1023 - 511" to generate a number in (-512, 512)?

##### Share on other sites
Yes exactly, i feel really stupid because i had been providing wrong values to the bound values so it would generate a bigger number than my terrain actually were, and when i then tried to get the height at that location it would naturally crash. (also fixed it by adding checks if(x > Width || z > Depth) return; ) :P