Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

10 different randoms 1

Status
Not open for further replies.

math

Programmer
Mar 21, 2001
56
BE
Hi,

Is there a way to easily get 10 DIFFERENT random numbers?

Thanx alot !!
Math
 
Hi Math
This is as simple as I could come up with in a few minutes, no doubt some one will do it in 2 lines, but this will give you approx 10 different integers 0 - 20 , but you must seed the generator to make sure you get a different set of numbers on each run.
You can use the system clock as a seed but a fast loop will beat this and you will get the same 10 numbers several times, also I would put an application.proccessmessages i the repeat/until loop.
Steve
 
Try

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
   loop: Integer;
begin
   Randomize;  // seed the random number generator
   for loop := 1 to 10 do begin
     Canvas.TextOut(100,100+(loop*30),InttoStr(Random(20)));
   end;
end;
Billy H

bhogar@acxiom.co.uk
 
Ah but Bill, he might get the same number more that once ?
He wants 10 Different numbers.
Steve.
 
In that case I would recommend the type of routine used in card game:

Shuffle the numbers from 1 to 100
then take the first 10

for example

Code:
procedure TForm1.Button2Click(Sender: TObject);
var
  nums : array[1..100] of integer;
  loop : integer;
  place1 : integer;
  place2 : integer;
  tmp1 : integer;

begin
  Randomize;
  for loop := 1 to 100 do
  begin
    nums[loop] := loop;
  end;

  for loop := 1 to 1000 do
  begin
     place1 := (Random(100)+1);
     place2 := (Random(100)+1);
     tmp1 := nums[place1];
     nums[place1] := nums[place2];
     nums[place2] := tmp1;
  end;
  for loop := 1 to 10 do begin
     Canvas.TextOut(100,100+(loop*30),InttoStr(nums[loop]));
  end;

end;
Billy H

bhogar@acxiom.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top