Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4098
Number of posts: 14002

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Unique Random Numbers Posted by Grafix on 21 Apr 2011 at 9:00 AM
Hi,
I am writing a program which involves generating 20 random numbers in the range of 1..100 and display them on the screen. However, each number must be unique so there should be no duplicates. So if a number has been genertaed, it cannot be generated again.

Below is my source code which I managed to write. However I have 2 problems with it. When i set the randmax to 100, a 216 run time error is given. When i set it to a low number such as 10, the program works but nothing is displayed.

Could anyone help please?

Edit: I finally managed to solve the problem; I had to do it in a different way but it worked. Thanks for your help it helped me a lot.

Program Rand_Unique;

Uses Crt;

Const
     arraymax=20;
     randmax=100;

Var
   i, j, rand : Integer;
   nums : Array[1..arraymax] of Integer;
   check : Boolean;

Begin
     TextBackground(White);
     TextColor(Black);
     Clrscr;

     j:=1;
     i:=1;
     nums[1]:=0;
     check:=False;

     Repeat

     rand:=random(randmax)+1;
     While (j<arraymax) or (check=False) do
     Begin
          If nums[j]=rand
             then check:=True
             else j:=j+1;
     End;
     If j=arraymax
        then Begin
                  nums[i]:=rand;
                  i:=i+1;
             End;
     Until(i=arraymax);

     For i:=1 to arraymax do
     Writeln(nums[i]);

     Readkey;
End.
Report
Re: Unique Random Numbers Posted by quikcarl on 21 Apr 2011 at 6:41 PM
There may be 2 things that I can think of that might help. I'm not sure what a 216 error is and maybe you should tell what it is.

      If j=arraymax

   begin
     randomize;      {this sets up the random number generator}
     .
     .
     .
     if j >= arraymax 

It could be that one of your variables is going beyond the range of the array index.
Report
Re: Unique Random Numbers Posted by Grafix on 21 Apr 2011 at 11:17 PM
Thank you for your suggestions. Those 2 comments really helped.

I had somehow fogot to randomize at the beginning.

About "j=arraymax"
That was surely wrong and your suggestion j>=arraymax makes much more sense so thanks again.

However, the error is still being given.
I googled run time error codes and error 216 is this:

216 General protection fault
Report
Re: Unique Random Numbers Posted by Grafix on 21 Apr 2011 at 11:19 PM
Thank you for your suggestions. Those 2 comments really helped.

I had somehow fogot to randomize at the beginning.

About "j=arraymax"
That was surely wrong and your suggestion j>=arraymax makes much more sense so thanks again.

However, the error is still being given.
I googled run time error codes and error 216 is this:

216 General protection fault
Report
Re: Unique Random Numbers Posted by Grafix on 21 Apr 2011 at 11:22 PM
Thank you for your suggestions. Those 2 comments really helped.

I had somehow fogot to randomize at the beginning.

About "j=arraymax"
That was surely wrong and your suggestion j>=arraymax makes much more sense so thanks again.

However, the error is still being given.
I googled run time error codes and error 216 is this:

216 General protection fault
Report
Re: Unique Random Numbers Posted by Grafix on 21 Apr 2011 at 11:27 PM
Sry I posted the same thing more than once but the website jammed for some time
Report
Re: Unique Random Numbers Posted by _Atex_ on 21 Apr 2011 at 8:15 PM
const max_no=20;
      max_value=100;

var a:array[1..max_no] of byte;
    cl:byte;

function check(b:byte):boolean;
 var trig:boolean;
     i:byte;
 begin
  trig:=false;
  for i:=1 to cl do if b=a[i] then begin trig:=true;break;end;
  check:=trig;
 end;


var i,j:byte;

begin
 randomize;
 fillchar(a,sizeof(a),0);
 cl:=0;
 for i:=1 to max_no do begin
  repeat j:=succ(random(max_value));until not(check(j));
  inc(cl);
  a[cl]:=j;
  writeln(j);
 end;
 readln;
end.

Report
Re: Unique Random Numbers Posted by Grafix on 21 Apr 2011 at 11:31 PM
Thank you, I copied your code and it worked.

However, could you please make some comments near important parts of the code cause I don't understand everything.

Firstly, I haven't ever used byte as a variable type and google doesn't explain much so if u can pls explain what's its use it would really help.

Secondly, I can't really manage to understand what the 2 global variables actually do, especially "cl".

Thanks for your reply and help.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.