iFocus.Life News News - Breaking News & Top Stories - Latest World, US & Local News,Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The iFocus.Life,

Dorin Duminica"s Entry for the Unique Random Numbers Generator

106 10
Question: Dorin Duminica's Entry for the Unique Random Numbers Generator

Challenge: a custom Delphi function, Randomizer, takes an open integer array and should fill it with unique random numbers as fast as possible.

The code is submitted for the Unique Random Numbers Delphi Challenge

Answer:

Unique random number generator entry by Dorin Duminica (Romania):

procedure Randomizer_Dorin_Duminica(const maxValue: int64; var values: array of int64) ; var   index: int64;   tmpValue: Int64;  function ValueExists(const AValue: Int64): Boolean;   var     k: int64;   begin     Result := True;     k := Low(values) ;     while k < High(values) do     begin       if values[k] = AValue then Exit;       k := k + 1;     end;     Result := False;   end; begin   index := low(values) ;   while index < High(Values) do   begin     tmpValue := random(maxValue) mod (maxValue or index) ;    while ValueExists(tmpValue) do       tmpValue := random(maxValue) mod (maxValue or index) ;    values[index] := tmpValue;     index := index +1;   end; end;

Test data:
- array size: 10 000
- number range: 1 - 100 000
Dorin's speed result: 625 milliseconds.

Explore the list of all accepted entries for the Fastest Unique Random Number Generator Delphi challenge.
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like on "Technology"

Leave A Reply

Your email address will not be published.