Author Topic: wrong usage of RandomGen ?  (Read 857 times)

titi

  • MegaGlest Team
  • Airship
  • ********
  • Posts: 4,240
    • View Profile
    • http://www.titusgames.de
wrong usage of RandomGen ?
« on: 12 January 2014, 00:03:26 »
My son is currently playing with a new feature that can make the camera shake like in an earthquake. While doing so he wanted to use random numbers and he used RandomGen.

In a method he did :
Code: [Select]
{
RandomGen randgen;
myRandom=randgen.randRange(0,max);
}

This always results in the same number! When I told him, and he printed out the number to see it, he told me that there are other places in the code where it is used like this ( Maybe I did one of this? )
Places that don't look good to me are:

properties.cpp line 482

and maybe worse:
ai_interaface.cpp line 679
in
Code: [Select]
bool AiInterface::isResourceInRegion(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, int range) const { ....

Do these places give the wanted behaviour?
Try Megaglest! Improved Engine / New factions / New tilesets / New maps / New scenarios

softcoder

  • MegaGlest Team
  • Battle Machine
  • ********
  • Posts: 2,239
    • View Profile
Re: wrong usage of RandomGen ?
« Reply #1 on: 12 January 2014, 00:21:52 »
A few things about RandomGen. Its deterministic, so really only meant to be used to get the same random # for all players. The code in ai interface was not added by me, and could use real randomization since the server controls the AI it does not need to use deterministic randomness. The use in properties is never used from my look at the code (it uses true randomness).

If you want to use the class and get different #'s, you can't use it as a local variable as it loses state every time it is used and goes out of scope, make it a member variable. You need to ensure it is not accessed multiple threads or it may cause out of synch.

will

  • Golem
  • ******
  • Posts: 783
    • View Profile
Re: wrong usage of RandomGen ?
« Reply #2 on: 16 January 2014, 12:11:52 »
RandomGen seems a good pseudo-random number generator; its just to call init(time(NULL)) before using it in AI and stuff.

 

anything