954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random in pascal

I have been using Pascal for around 3-5 months now. I still haven't figured out how to return random answers. for instance there are 5 integers:

var a,b,c,d,e : integer;

and now the computer will tell you a random integer like:

writeln(c);

this is just an example of course because i would then later want to use it for other stuff like games. i wouldn't just want to display the text. can anyone help please?

killhha
Light Poster
26 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 
const
  myarray: array [0..9] of Integer = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
begin
  Randomize;
  Writeln(myarray[Random(10)]);
end;
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

Another example?

Program RandomNumbers;
Uses Crt;
Var  A,B,C,i:Integer;{-32767..32768}
Begin {main}
     ClrScr;
     Randomize;        {shuffling}
     For i:=1 To 3 Do {this just that see the results for three times}
        Begin
           A:=Random(20); {tis will generate one number from 0 to 20}
           WriteLn(A);    {write the results..}
           B:=Random(20);
           WriteLn(B);
           C:=Random(20);
           WriteLn(C);
           WriteLn;
        End;
     ReadKey;
End. {main}

(*Created By FlamingClaw 2009.03.13*)
FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

thanks for the help guys! :D but is it possible to just randomize 1 variable?

killhha
Light Poster
26 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 

Ofcourse. Just declare one integer, call randomize and then random.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

Deleted

killhha
Light Poster
26 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 
Ofcourse. Just declare one integer, call randomize and then random.

sorry im such a noob but how would i do this if i have lets say 5 integers and i wanted to make one of them random because the others hold important data? could you write an example program please?

killhha
Light Poster
26 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 

The answers were before you,just exchange the type of variables...
What kind of type of inportant data?

Program Variables;
Uses Crt;
Var a:String;{texts}
    b:Integer;{-32767..32768}
    c:Byte;{0..255}
    e:Word;{0..65535}
    {etc}
{first you must learn the basics!!!!}

Begin
   ClrScr;{clear screen}
   a:='Turbo Pascal';
   Randomize;{shuffling}
   b:=Random(3000);
   c:=13;
   e:=42000;
End;

{Created by FlamingClaw 2009.03.14}
FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

oh i see! so randomize wont randomize everything. the variables will only be randomized when you include the line random like so:

a:=random(20);


am i correct?

killhha
Light Poster
26 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 

ok i just tested it out. it is. thanks for all your help guys!

killhha
Light Poster
26 posts since Mar 2009
Reputation Points: 10
Solved Threads: 1
 
{ With or without Randomize; }
{Last example...}
Program Difference;

Uses Crt; {used unit}

Var  Number:Byte; {0..255,this is the range}
     index1,index2:Byte;

Begin {main}
     ClrScr;
     Write('When you press any key,');
     WriteLn('then we run our program without randomize:');
     ReadKey;
     For index1:=1 To 3 Do
        Begin
           WriteLn;
           For index2:=1 To 3 Do
              Begin
                 {any number from 0 to 255}
                 Number:=Random(255);
                 {write it to the screen,three times per round}
                 WriteLn(Number);
                 {write this numbers down on a paper}
              End;
        End;

     ReadKey;
End. {main}

{

-= Note By FlamingClaw =-

I see,that you begin to understand...
If you write down the above numbers that generates by the
Random(255),only nine numbers..:)
and run this program again and again....you will see that
the numbers are always unvaried...
But if we write before of Random(250) a 'Randomize;' command
and run this little program about two times,then we'll see
the difference,because the results will be different each run
I think,this is the best way to understand this mechanism

 }

(*Created By FlamingClaw 2009.03.14*)
FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

i dont understand this program

aminazineb
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

hi
could u show me a randomize in string plz as i wanted to randomize peoples names on pascal

Matthew64
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

You'd better start a new thread for this. Specify what you need, and show your code so we can see what's wrong.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You