seeding randomly but can still do the same every time

Started by
4 comments, last by the dodger uk 11 years, 1 month ago

i need to place objects in a scene via a height map ,

height maps are simple rows and collums.

the problem is if i place objects via this then it will be in colloms and rows and it can be seen , so it will look rubbish

is there a way to do it so it looks random and better placed but i will still get the same results every time i run the program

?

Advertisement

So, are you just looking to generate new random numbers every different time? What language and what OS are you using?

In C, rand() will give you the same results every time based off of how long it takes to initialize the OS (I think), and even that can be OS-dependent (this is coming from a Linux background). You should be able to, however, change those results by seeding.

Check out srand(), if you're using C/C++. You can feed it time(NULL), which will gives the elapsed time in seconds since Jan 1st, 1970. In other words, the value changes just about every single time you call time(...), and feed the returned result to srand(), which then generates a new sequence of random values that can be retrieved by calling rand().

Here's a link: http://www.cplusplus.com/reference/cstdlib/srand/

What I did was call this in my game's initialization code:


srand(time(NULL));

Just call rand() like your normally do, and things should look differently from there. If you start seeing similar results, try calling that line in a few other places, but don't overuse it --it can be a heavy system call.

If you want a random terrain, but be able to reload that same terrain, then base your generation off of rand() (if you are using C/C++), and seed the first load with the time, as Vincent_M stated above. Save the value of the seed and just load that same seed number to regenerate the same terrain.

-EDIT-

Misread post

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

You want random positions on a tile-based map, but you don't want the positions to be locked to the tiles, right? The best way to do that is to generate numbers on a larger scale than your grid-based map is on. For example, if your map is a 20x20 grid, generate random numbers from, say, 0-400 for the coordinates, then divide them by 20. This will get you random decimal coordinates between 0-20.

And if you want the same results every time you run the program, you do not want to call srand, or you want to call it with constant number as a parameter.

cheers for the input , im not sure about the methods that are mentioned , but i think i have another way around it

use the hightmap , and then use those cords on another height map which uses the grid thing above and down scales it by the 20 or so

This topic is closed to new replies.

Advertisement