View Single Post
  #8   Spotlight this post!  
Unread 31-07-2012, 17:29
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,093
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Replacing duplicates in a randomly generated array


Simple (and very fast) TP7 implementation:

Code:
{ Choose 20 numbers from the set [1..10000] with no duplicates }

CONST
size=10000;
choose=20;

VAR
a: array[1..size] of word;

Procedure Initialize;
var i: word;
begin
for i:=1 to size do a[i]:=i;
end;

Procedure Shuffle;
var i,j,temp: word;
begin
for i:=1 to choose do
  begin
  j:=random(size)+1;
  temp:=a[i]; a[i]:=a[j]; a[j]:=temp;
  end;
end;

Procedure Select;
var i: word;
begin
for i:=1 to choose do writeln(a[i]:8);
end;

CONST
cr=#13#10;

BEGIN

write('Initializing...');     Initialize;
write(cr,'Seed the RNG...');  Randomize;
write(cr,'Shuffle...');       Shuffle;
write(cr,'Select...',cr);     Select;
write('done. press ENTER ');  readln;

END.

Last edited by Ether : 31-07-2012 at 17:38.