0
\$\begingroup\$

In my current situation I have a game world, in that game world I want to place loads of tree models. It is a waste of time for me to actually explicitly define the x y and z values of each tree model, so I want to do this with the help of random logic, so that it can just set the values for me. I don't want true randomness, so I do not need to seed the random logic, I want to generate the positions of all the trees randomly so that I don't need to type each one myself, but each time I run the game the trees are in the same position, so it is not truly random but that is what I want.

My current attempt is as follows:

for (int i = 0; i < numOfTrees; i++) {
        treeObjects.push_back(
        (new Entity(Vector3f(//x, y and z position value of each tree
                             rand() % 200 + (-200), //random value for x  
                             0.0f,                  //y value kept as 0.0f
                             rand() % 200 + (-200)) //random value for z
//above code positions the trees, the rest of the code is irrelevant
}

The issue with this is that all the trees are very tightly packed together, so I am very skeptical that they are getting a value between -200 and 200, because if they were they would be much more spread out. Does someone know of a better solution to produce what i'm after? maybe there is an issue with the way I am doing it?

\$\endgroup\$
  • 1
    \$\begingroup\$ I printed out the x and z value of each tree and they are all negative, so it looks like my above logic generates random floats between 0 and -200, instead of 200 and -200? \$\endgroup\$ – Kyle_Pearce Feb 28 '20 at 14:30
1
\$\begingroup\$

rand() % 200 yields a random number between 0 & 200.

rand() % 200 + (-200) takes the range above and shifts; think of it as adding -200 to both the min possible outcome & the max possible outcome.

Thus, 0 to 200 becomes -200 to 0.

To get a -200 to 200, first, note the size of the range is 400 units. So you start with rand() % 400. Next shift it so that half the outcomes are negative: (rand() % 400) - 200

\$\endgroup\$
0
\$\begingroup\$

Thanks to anyone that took the time to answer my question, but I wanted to answer it myself for future people interested in a potential solution - I replaced each instance of rand() % 200 + (-200) In my question code (for the randomized x and z values of the Vector3f object) with the following:

-200 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(200-(-200))))

And now this code correctly generates floats between -200 and 200.

\$\endgroup\$
  • \$\begingroup\$ Welcome to GDSE. Glad you were able to solve your problem & answering your own question is fine. As currently written though, this is a link only answer, which is susceptible to link rot (even if it's to an answer on SO) and tends to attract down votes. You could improve it by editing to include enough details for others to use your answer even if the link goes dead. \$\endgroup\$ – Pikalek Feb 28 '20 at 15:30
  • 2
    \$\begingroup\$ Also note that the accepted answer there is terrible, given how C++ provides much better tools for generating random numbers than rand(), as indicated by this answer: stackoverflow.com/a/17798317/3450269. \$\endgroup\$ – Tyyppi_77 Feb 28 '20 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.