1. Home
  2. Computing & Technology
  3. Delphi Programming

Challenge: Fastest Unique Random Number Generator
Delphi Challenge: Fill an Integer Array with Unique Random Numbers

By , About.com Guide

delphi random numbers

delphi random numbers

Delphi programming challenges / exercises are designed to help you refactor your old code to make it more efficient; get new ideas on how to code faster / better Delphi code; help you solve a particular Delphi programming task and of course: to have fun while coding in Delphi :)

Challenge: Fastest Unique Random Number Generator

Your challenge is to code a custom Delphi procedure with the following signature:
procedure Randomizer(const maxValue : int64; var values : array of int64) ;
Randomizer takes 2 parameters: values is an int64 open array. Your code must fill in the "values" array with unique random numbers from 1 to maxValue.

For example, if maxValue is 10 and the length of the array is 5,
a valid entry would be [2,3,1,8,7]
a non - valid entry is [2,4,6,4,10] - two 4's.

The following code should be used to prepare the array and call the Randomizer procedure:

uses Math;

const
  sizeExponent = 2; // 1 or 3 or 4 or 5 or ...
var
  arraySize : int64;
  maxValue : int64;
  values : array of int64;
begin
  arraySize := Trunc(Power(10, sizeExponent)) ;
  maxValue := 10 * arraySize;
  SetLength(values, arraySize) ;

  Randomizer(maxValue, values) ;

  // "values" array should be filled with
  // unique random numbers from 1 to maxValue

end;

Here are a few hints:

Accepted Entries (so far)

Here's the list of the accepted entries. 14 entries were accepted before initial results were published.

Note: the list is in the order of execution speed, with the best results on top.

99 microseconds by Patrick van Logchem
WINNER - 10 "fame and glory" points! :)

Hrvoje Brozović - 353 microseconds
David Reed - 370 microseconds
Kaushalya Damitha Weerakoon - 860 microseconds
Peter Kras - 3300 microseconds
Jim Murtha - 3 600 microseconds
Stefan Tashev - 6150 microseconds
Jim McKeeth - 125 milliseconds
Marius van Loo - 130 milliseconds
Willie Geldenhuys - 130 milliseconds
Didier Cabalé - 350 milliseconds
Harry Stockx - 400 milliseconds
Dorin Duminica - 625 milliseconds
Korhan Taþçýkar - 3 seconds
Note: test are done on a Pentium 2.4, 2GM RAM machine with Windows XP Pro installed.

To test the speed of execution the TStopWatch - High Precision Timer class was used.

Challenge CLOSED

Note: this challenge is closed. Several valid entries have been submitted before the challenge was closed and the winner(s) announced.

Delphi programming challenges / exercises are designed to help you refactor your old code to make it more efficient; get new ideas on how to code faster / better Delphi code; help you solve a particular Delphi programming task and of course: to have fun while coding in Delphi :)

Participate in active Delphi programming challenges

More Delphi Programming Quick Tips
Readers Respond: Your Delphi Programming Challenges
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2009 Tips
  7. Delphi Challenge: Unique Random Number Generator - a Custom Delphi Function to Fill an Array with Unique Random Numbers

©2009 About.com, a part of The New York Times Company.

All rights reserved.