# question about a type in pascal

Hi,
it's a simple question i know but it's the base of all my program
i have to take a random number out of 2, 3, 5 and 7
how do i proceed ???

temp:=Random(2, 3, 5, 7);
doesn't seem to work

a type ??
TYPE
Primes=(2, 3, 5, 7);
a get a message error asking for a type or something like that
"Identifier Expected"

thanks a lot

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
I would generate a random number between 1-4 and use that as an index into an array with those values

type
P_array =  array[1..4] of integer;

var
Rand_Index  : integer;
Primes : P_array;

begin
Primes [1] := 2;
Primes [2] := 3;
Primes [3] := 5;
Primes [4] := 7;

randomize;
Rand_index := 4*random +1;

writeln (Primes[Rand_Index]);

end.

mlmcc

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Self-employed developerCommented:
Good answer, mlmcc. However, I would suggest some minor alterations:

1) use an "P_array = array[0..3] of Integer;".
2) use "Rand_index := Random(4);"

And perhaps the Pascal version used can define arrays of constants like this:

const
Primes : P_array = (2, 3, 5, 7);

Good answer, though. You deserve all credit in my opinion. ;-)
Commented:

Alex:
I grew up with arrays being 1-based so I tend to keep them like that.

Does Random(4) give a number from 0 to 3.999999?

I don't think Pascal allows constant arrays but it might.  Been too long since I used them.

mlmcc
Self-employed developerCommented:
mlmcc:
I know, in Basic arrays used to be 1-based. However, when I learned Pascal a couple of decades ago I was told that arrays have user-definable upper and lower bounds. I do prefer to use 0-based arrays, though. Don't know why but it's just a preference...

Random(4) returns either 0, 1, 2 or 3. Nothing more, nothing less. At least in Turbo Pascal and Delphi.

I know Delphi allows constant arrays. I even use them quite often. I'm not sure but I think Turbo Pascal allows constant arrays too. But it's too long ago that I wrote my last TP application.
Commented:
Alex: In VB arrays are 0-based.  I learned in FORTRAN IV.  I used to teach Pascal and we generally used arrays as 1 based.  I know you can set the index to be anything (one of the features of Pascal I really like).

mlmcc

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.