1
Programming / Re: I don't understand random seeds.
« on: March 20, 2014, 07:29:21 AM »Okay, so I basically need to figure out how to set the Seed for the RNG then any rand functions that come from it will always be the same. (I may need to test this out on some small tests). I would love if I could save a 6 digit number and always have the same world.
This might help. It's a slice from the tutorial code I'm using to build my own game (it uses the libtcod library):
Map::Map(int width, int height)
: width(width),height(height) {
seed=TCODRandom::getInstance()->getInt(0,0x7FFFFFFF); //The 'seed' is simply a pseudo-random number, selected by an ordinary generator. It can be any value (just remove the call to TCODRandom and enter your own number).
}
void Map::init(bool withActors) {
rng = new TCODRandom(seed, TCOD_RNG_CMWC); // 'rng' uses the number generated by 'seed'.
tiles=new Tile[width*height];
map=new TCODMap(width,height);
TCODBsp bsp(0,0,width,height);
bsp.splitRecursive(rng,8,ROOM_MAX_SIZE,ROOM_MAX_SIZE,1.5f,1.5f);
BspListener listener(*this);
bsp.traverseInvertedLevelOrder(&listener,(void *)withActors);
}
//Below is just an overview of how 'rng' is used to generate a room using BSP nodes.
// 'map.rng->getInt' uses the seeded value, along with a couple of other variables, to create rooms of a size somewhere between ROOM_MIN_SIZE and the maximum size of that room's BSP node, and to set the room's x/y coordinates.
w=map.rng->getInt(ROOM_MIN_SIZE, node->w-2);
h=map.rng->getInt(ROOM_MIN_SIZE, node->h-2);
x=map.rng->getInt(node->x+1, node->x+node->w-w-1);
y=map.rng->getInt(node->y+1, node->y+node->h-h-1);
As long as the seed is set to the same number, 'rng' will always create the same BSP tree and generate the same map layout. The call to TCODRandom() in Map::map ensures that the seed will always be different.