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?

Recommended Answers

All 13 Replies

const
  myarray: array [0..9] of Integer = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
begin
  Randomize;
  Writeln(myarray[Random(10)]);
end;

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*)

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

Deleted

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?

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}

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?

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

{ 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*)

i dont understand this program

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.