Author Topic: Random is not random  (Read 1300 times)

0 Members and 1 Guest are viewing this topic.

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Random is not random
« on: August 25, 2007, 01:50:12 pm »
Okay... I use the command Random to generate random numbers for me script. However, I have noticed that the numbers this command produces are not random. If I run the script several times in a row, the numbers stay the same! This leads me to believe that the random number generator is not seeded with something different every time the soldat server application starts. In other programming applications you usually seed the random number generator with the current computer clock time to get pseudorandom numbers.

My question: Is there a command to seed the random number generator? If not, can it please be added to the next version?



Date Posted: August 25, 2007, 02:21:08 PM
nvm ... I saw we have time so I sorta worked around it by getting the current time and generating as many random numbers as the current time in milliseconds on activateserver. Seeding would still be nice though.
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline spkka

  • Camper
  • ***
  • Posts: 469
Re: Random is not random
« Reply #1 on: August 25, 2007, 02:45:36 pm »
well thats what i call random!!  [retard]

Offline Quintinon

  • Major(1)
  • Posts: 4
Re: Random is not random
« Reply #2 on: August 25, 2007, 02:49:05 pm »
Its seeded by time ... if you call it a lot in a small amount of time it will be the same, if you want it to be different, just use your own random() function that uses rand() in it, but has a variable that it  * / + - % 's by that changes every time, still seeded by time, but also by your own variable ;)

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: Random is not random
« Reply #3 on: August 26, 2007, 07:40:01 am »
Thanks but I don't think it works that way in soldat. If it was seeded by computer uptime (which is indeed what people often do) I wouldn't get the same values every time I restart the server. This would be the case if it would be seeded by the time the server is running.

Also, there is no rand function in soldat scripting ;). I just tried it in ActivateServer();
07-08-26 14:34:19 
  • default -> [Error] (2579:2): Unknown identifier 'rand'


I just circumvented it now by asking for n random numbers where n is the current number of milliseconds. Since this is different every time I start the server, you start in a different point in the pseudorandom list. So I guess it works fine enough now. thanks though
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline nub

  • Major
  • *
  • Posts: 56
Re: Random is not random
« Reply #4 on: August 26, 2007, 10:07:57 am »
I made up my own seed by taking coordinates of players on each kill.
Code: [Select]
  GetPlayerXY(Victim,x,y);
  SEED := round(x*y*100);
  GetPlayerXY(Killer,x,y);
  SEED := SEED*round(x+y*100);

And this is my random:
Code: [Select]
function Random(x:integer):longint;
var y: longint;
  p : integer;
  i : integer;
begin
  p := strtoint(FormatDate('s'));
  for i:= 0 to p do begin
    y := SEED*p+i;
  end;
  if (y<0) then y := -y;
  result := y mod x;
end;

Можно зарезать, украсть и всё-таки быть счастливым...

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: Random is not random
« Reply #5 on: August 27, 2007, 07:48:14 am »
Fixed in next version.

Forgot one simple line, "Randomize;" before calling the internal rand function -_-

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: Random is not random
« Reply #6 on: August 27, 2007, 08:29:51 am »
Hehe, it happens ;)

Thanks for fixing it!
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com