RSS Forums RSS
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1323 | Replies: 6 | Solved
Reply
Join Date: Dec 2007
Posts: 7
Reputation: simps0n is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
simps0n simps0n is offline Offline
Newbie Poster

Generate N random different numbers

  #1  
Apr 12th, 2008
Hello, guys!

I have to generate 10 different numbers.
What I do is to store the generated numbers in array.
Before that I check the array for duplicates.

It compiles, but I get some duplicates.
Thanks in advance for any help.
  1. program rand;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. var
  9. j, i, a, m: integer;
  10. number: array[1..10] of integer;
  11. included: boolean;
  12.  
  13. begin
  14. j := 1;
  15. m := 1;
  16.  
  17. while j < 11 do
  18. begin
  19. randomize;
  20. a := random(11);
  21.  
  22. // Checking array for duplicates.
  23. for i := 1 to 10 do
  24. begin
  25. if (number[i] = a) or (a=0) then
  26. begin
  27. included := true;
  28. end
  29. else
  30. begin
  31. included := false;
  32. end; {if loop}
  33. end; {for loop}
  34.  
  35. // Adding number to array.
  36. if not included then
  37. begin
  38. number[m] := a;
  39. m := m + 1;
  40. j := j + 1;
  41. end; {if loop}
  42. end; {while loop}
  43.  
  44. // Printing out all numbers in array.
  45. for i := 1 to 10 do
  46. begin
  47. writeln(number[i]);
  48. end;
  49. readln;
  50. end.
Last edited by simps0n : Apr 12th, 2008 at 7:50 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 11
Reputation: DevonMcC++ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
DevonMcC++ DevonMcC++ is offline Offline
Newbie Poster

Re: Generate N random different numbers

  #2  
Apr 13th, 2008
The problem is that, apparently, what you really want is a permutation - it looks like you want only 10 random numbers between 1 and 10. There are any number of ways to do this. One is to go to jsoftware.com, download and install J, then type "1+10?10" in a session.

However, since this is a "Pascal and Delphi" forum, the answer will be substantially more complicated than this.

Here's my guess, allowing for my lack of Pascal knowledge: initialize the array of numbers 1 to 10, then loop through, say 10 times (this is probably overkill), swapping numbers randomly, something like this code fragment:

for i := 1 to 10 do
begin
number[i] = i;
end; (for loop)
for i := 1 to 10 do
begin
ix1 := 1+random(10);
ix2 := 1+random(10);
svnum := number[ix1];
number[ix1] := number[ix2];
number[ix2] := svnum;
end; (for loop)

Since you're swapping (roughly) 2 each time, you could shorten the loop to as little as 5 - somewhere between 5 and 10 would be optimal.
Reply With Quote  
Join Date: Jan 2008
Location: definately not on moon :P
Posts: 65
Reputation: another guest has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 4
another guest another guest is offline Offline
Junior Poster in Training

Re: Generate N random different numbers

  #3  
Apr 13th, 2008
SysUtils is a turbo pascal unit?
Reply With Quote  
Join Date: Jun 2006
Location: Blumenau, Brazil
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: Generate N random different numbers

  #4  
Apr 17th, 2008
Originally Posted by simps0n View Post
It compiles, but I get some duplicates.

Hi simps0n,

Without taking in consideration any others questions, You must to call randomize procedure just an once time in Your project (take a look in help about it).

So, put the call to this procedure before while .. do scope, like this:
  1. ...
  2. m := 1;
  3. randomize;
  4. while j < 11 do
  5. begin
  6. a := random(11);
  7. ...

Bye
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote  
Join Date: Oct 2007
Location: South Africa
Posts: 60
Reputation: RoryGren is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
RoryGren's Avatar
RoryGren RoryGren is offline Offline
Junior Poster in Training

Re: Generate N random different numbers

  #5  
Apr 22nd, 2008
What is also happening in your code, is that when you find a duplicate, you are setting included to True, but are not exiting your loop and the next number in the array is not a duplicate, so included is set back to false.
You just add an additional line to exit the loop when included is set to true.

// Checking array for duplicates.
for i := 1 to 10 do
begin
  if (number[i] = a) or (a=0) then
  begin
     included := true;
     exit;
  end
  else
  begin
     included := false;
  end; {if loop}
end; {for loop}

Just a bit about the begin ... end in your code -

You only need to wrap the commands in those if there is more than one command to be performed - in the above example, if the test is true you are going to perform 2 or more commands, so you need the begin ... end. If it is false, you are running only one command, so you don't need them, so the code could look like this:
// Checking array for duplicates.
for i := 1 to 10 do
begin
  if (number[i] = a) or (a=0) then
  begin
     included := true;
     exit;
  end
  else included := false; {end if loop}
end; {for loop}
Reply With Quote  
Join Date: Dec 2007
Posts: 7
Reputation: simps0n is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
simps0n simps0n is offline Offline
Newbie Poster

Re: Generate N random different numbers

  #6  
Apr 22nd, 2008
Thank you all, guys!
Really appreciated.
Reply With Quote  
Join Date: Feb 2008
Posts: 11
Reputation: DevonMcC++ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
DevonMcC++ DevonMcC++ is offline Offline
Newbie Poster

Re: Generate N random different numbers

  #7  
Apr 24th, 2008
Originally Posted by simps0n View Post
Thank you all, guys!
Really appreciated.


I hope you noticed, though no one commented on it, that the suggestion I made scales much better than the original algorithm. That is, if you're doing this for 10,000 numbers instead of 10, the original algorithm will take about a million times as long versus about a thousand times as long for what I proposed.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:19 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC