943,583 Members | Top Members by Rank

Ad:
Apr 12th, 2008
0

Generate N random different numbers

Expand Post »
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.
Pascal Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simps0n is offline Offline
7 posts
since Dec 2007
Apr 13th, 2008
0

Re: Generate N random different numbers

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
DevonMcC++ is offline Offline
16 posts
since Feb 2008
Apr 13th, 2008
0

Re: Generate N random different numbers

SysUtils is a turbo pascal unit?
Reputation Points: -7
Solved Threads: 4
Junior Poster in Training
another guest is offline Offline
66 posts
since Jan 2008
Apr 17th, 2008
0

Re: Generate N random different numbers

Click to Expand / Collapse  Quote originally posted by simps0n ...
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:
delphi Syntax (Toggle Plain Text)
  1. ...
  2. m := 1;
  3. randomize;
  4. while j < 11 do
  5. begin
  6. a := random(11);
  7. ...

Bye
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
Micheus is offline Offline
72 posts
since Jun 2006
Apr 22nd, 2008
0

Re: Generate N random different numbers

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.

Pascal and Delphi Syntax (Toggle Plain Text)
  1. // Checking array for duplicates.
  2. for i := 1 to 10 do
  3. begin
  4. if (number[i] = a) or (a=0) then
  5. begin
  6. included := true;
  7. exit;
  8. end
  9. else
  10. begin
  11. included := false;
  12. end; {if loop}
  13. 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:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. // Checking array for duplicates.
  2. for i := 1 to 10 do
  3. begin
  4. if (number[i] = a) or (a=0) then
  5. begin
  6. included := true;
  7. exit;
  8. end
  9. else included := false; {end if loop}
  10. end; {for loop}
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
RoryGren is offline Offline
60 posts
since Oct 2007
Apr 22nd, 2008
0

Re: Generate N random different numbers

Thank you all, guys!
Really appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simps0n is offline Offline
7 posts
since Dec 2007
Apr 24th, 2008
0

Re: Generate N random different numbers

Click to Expand / Collapse  Quote originally posted by simps0n ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
DevonMcC++ is offline Offline
16 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Convert Delphi Packed Record to C#
Next Thread in Pascal and Delphi Forum Timeline: 'Abstract Error' while closing the form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC