1. Computing & Technology

Discuss in my forum

in Delphi Code Challenges :: 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 :)

This time, 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.

Read the full article to learn how to submit your solution to this Delphi Challenge: Unique Random Number Generator

To win, submit the fastest entry!

Related:

Comments
June 9, 2009 at 11:55 am
(1) Eric :

You should probably qualify the degree of “randomness” expected: a low quality “looks like random from a mile away” implementation will be much cheaper than a truly random implementation.

June 9, 2009 at 1:45 pm
(2) Jim McKeeth :

@Eric: I was going to suggest the same thing. Are we expected to just use the random methods built into Delphi (as per the link to Generating random numbers).

Of course I guess we should handle the case in which more numbers are requested than the range allowed. Seems that should be invalid too.

June 9, 2009 at 3:39 pm
(3) Zarko Gajic :

@Eric: The standard Delphi Randomize and Random functions would be ok.

@Jim: Note that the max random number is always Length(array) * 10. This comes from the preparation code in the challenge description.

June 9, 2009 at 4:08 pm
(4) Brad Phipps :

This reminds me of an old joke.

function Random():int;
begin
Result:=3;
// number picked randomly,
// chosen by throw of dice.
end;

;-)

June 9, 2009 at 10:13 pm
(5) wouter :

http://xkcd.com/221/

Alt-text: “RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.”

:)

June 10, 2009 at 1:56 am
(6) Eric :

@Zarko: I was also referring to making use of the unqualified randomness properties to take “shortcuts”.

f.i. instead of generating a random number in the whole range and checking its uniqueness, a different randomness version could split the range in segments and generate a number in each segment, thus eliminating the need for an explicit uniqueness validation. It would still look and be random, just with different randomness properties :)

June 10, 2009 at 3:34 am
(7) Zarko Gajic :

@Eric: As long as no numbers are repeating and every time you run the procedure you do get different results (”looks” random) it’s ok.

Let’s see that code and how fast that would be…

June 10, 2009 at 2:06 pm
(8) Jim Murtha :

If you call the routine multiple times you get a different set of numbers each time, if you restart the program at each run you will always get the same numbers. Should I resend it with a Randomize statement or am I good without it?

June 11, 2009 at 6:39 am
(9) Zarko Gajic :

@Jim Murtha: Let’s say that you are good without the call to “Randomizer” – only for this challenge. In real world you would, of course, ensure that random numbers are randomly seeded.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2012 About.com. All rights reserved.

A part of The New York Times Company.