0

I want to create a forest like terrain using OpenGL and C++ in Visual Studio. Naturally, I want to place objects such as trees or bushes at random so it looks more natural. I wanted to do this by using the rand() function, but by using it, while the program is running, my objects are constantly changing location. I get that the program is in loop and because of that rand() is acting the way it is. I wanted to ask you guys if there is a good alternative to this. I just want to get a random location value for each object upon his creation and that that value stays constant. Here is a small part of the code which has this issue:

for (int i = -5.0; i < 5.0; i++){
    for (int j = -5.0; j < 5.0; j++){
        double k = i * 10.0 + ((rand() % 20 + 10) + ((rand() % 8 + 1) / 10.));
        double l = j * 10.0 + ((rand() % 20 + 10) + ((rand() % 8 + 1) / 10.));
        if (i % 2 == 0 && j % 2 == 0)
        {
            drvo(k, l);
        }
    }
}

Any help I get will be much appreciated!

xaxxon
  • 19,189
  • 5
  • 50
  • 80
Daelendil
  • 47
  • 1
  • 13
  • 2
    Call rand() to get your random location, and then store that location value somewhere for future re-use (rather than calling rand() again for every object for every frame). That way your objects will be randomly placed but stay in the same spot. – Jeremy Friesner May 30 '17 at 05:53
  • 1
    Try to use std::uniform_real_distribution from C++11 like here https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library – Rostyslav Fylyshchuk May 30 '17 at 06:49
  • @JeremyFriesner, i don't think i quite understood you. I added some lines outside the function which renders the scene: ` int c = rand() % 20; int a = c; int d = rand() % 10; int b = d;` And changed the code a little bit: ` for (int i = -5.0; i < 5.0; i++){ for (int j = -5.0; j < 5.0; j++){ double k = i * 10.0 + a + 10 + (b + 1) / 10.; double l = j * 10.0 + b + 10 + (a + 1) / 10.; if (i % 2 == 0 && j % 2 == 0) { drvo(k, l); } } }` This method doesn't work since it always loads the same value. I am sure that i misunderstood you at some point. – Daelendil May 30 '17 at 09:21
  • You need to generate a separate set of random co-ordinates once for each object, and store that result with the object (or in an array, or something) so that you can reference the object's co-ordinates each time you redraw the object. – Jeremy Friesner May 30 '17 at 14:19
  • Thank you, I solved the issue. – Daelendil May 30 '17 at 15:17

1 Answers1

1

rand is not fully random, it's pseudorandom. Meaning that given the same seed, rand will produce the same values.

I suggest using srand to seed the PRNG with a seed that you save for later use.

When you want to render the scene again, call srand again with your saved seed and the sequence of rand calls should yield the same results.

arboreal84
  • 2,086
  • 18
  • 21
  • Correct me if I'm wrong. But as I understand, every computer generated number is pseudo-random number. Because every random number generation algorithm are predefined and works on known inputs. – cse May 30 '17 at 07:19
  • No computers can generate truly random numbers. Computers are deterministic and any algorithm for generating randoms really generate cyclic sequences of numbers (what differentiate good from bad generators is the length of those cycles). Real sources of randomness are not that common or easy to use (a good one is given by radioactive material). – Davide Spataro May 30 '17 at 07:28
  • And my answer is not in contradiction with that. In a programming setting you need to pick your entropy source carefully if you rely on randomness (e.g: gambling, cryptography). There are commercially available devices that allow you to generate entropy as input for RNGs. – arboreal84 May 30 '17 at 07:28
  • @Daelendil if this helped you please select it as answer – arboreal84 May 30 '17 at 15:41
  • It didn't, for some reason, the srand function wouldn't work properly. – Daelendil May 31 '17 at 06:40