Recent

Author Topic: Random() returns same value if called twice  (Read 6151 times)

ezlage

  • Guest
Random() returns same value if called twice
« on: June 30, 2011, 09:33:32 am »
Friends,

With the below code, if I call the function twice it returns same values. But it should return different values.

Code: [Select]
function GeraChave(tipo: string; tamanho:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} export;
var
  i:integer;
  chave:string='';
  sort:string;
begin
  if (tamanho>=10) and (tamanho<=30)
    then begin end
    else begin
      randomize;
      tamanho:=random(21)+10;
    end;
  case AnsiIndexStr(LowerCase(tipo),['bin','oct','dec','duo','hex','afncs','afnci']) of
    0: sort:='01';
    1: sort:='012345678';
    2: sort:='0123456789';
    3: sort:='0123456789AB';
    4: sort:='0123456789ABCDEF';
    5: sort:='1234567890NOPQRSTUVWXYZabcdefghijklmn01234567891234567890opqrstuvwxyzABCDEFGHIJKLM0123456789';
    else sort:='0123456789abcdefghijklmnopqrstuvwxyz1234567890';
  end;
  randomize;
  for i:=1 to tamanho do begin
    chave:=chave+sort[random(length(sort))+1];
  end;
  result:=chave;
end;
Gera=Make, Chave=Key, Tipo=Type, Tamanho=Length.


For example:
Code: [Select]
GeraChave('bin',10); It twice should return different values, but not.

Code: [Select]
GeraChave('bin',random(10));
GeraChave('oct',random(10));
That way, returns different values, but always with same length. The length should be random.

How to solve this problem? what I did wrong?

Sorry by my poor English.
Tanks.
« Last Edit: June 30, 2011, 09:40:16 am by ezlage »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Random() returns same value if called twice
« Reply #1 on: June 30, 2011, 11:17:00 am »
Only call randomize;  once when program starts.

I mean, what it does is same as  randseed:=Gettickcount;

This means that you get same randseed for many random() calls, which always gives same value, because the random() is called during same millisecond.
« Last Edit: June 30, 2011, 11:19:51 am by User137 »

Bart

  • Hero Member
  • *****
  • Posts: 5690
    • Bart en Mariska's Webstek
Re: Random() returns same value if called twice
« Reply #2 on: June 30, 2011, 11:18:26 am »
You should call Randomize only once.
It sets RandSeed, the startingpoint for calculations of Random() values.
In Randomize Randseed is calculated using the current systemtime.

If you call Randomize twice within a very short timeframe (so that system time does not change), it will set RandSeed to the exact value as before.
So if you call Random(), you get the same value as the first time.

Call Randomize once, at program start.
That should do the trick.

Bart

[edit]User137 beat me to it...[/edit]

ezlage

  • Guest
Re: Random() returns same value if called twice
« Reply #3 on: June 30, 2011, 05:59:32 pm »
I will test here.
Friends, I hope someday help you like you are helping me.

Sorry by my poor English, again!

Thanks!

ezlage

  • Guest
Re: Random() returns same value if called twice
« Reply #4 on: July 01, 2011, 06:38:57 am »
Is working now!
Thanks!

 

TinyPortal © 2005-2018