r/delphi • • Nov 08 '21

A for loop question

Hi i just wanna ask if a for loop runs 7 times to get 7 random numbers. How do i get each random number apart from the loop to process? PLS help me

0 Upvotes

10 comments sorted by

3

u/JimMcKeeth Delphi := ∞; Nov 08 '21

Store the numbers in an array

1

u/[deleted] Nov 08 '21

Is this the only way because im not there yet

3

u/JimMcKeeth Delphi := ∞; Nov 08 '21

There are a lot of different ways, but that is probably the simplest. If you just want to display them then you can call Write in the loop, but then you can't access the numbers anymore.

Another option is to create a TList object. You could also use a Stack or Queue. Maybe write them to a file and read them back in.

1

u/[deleted] Nov 09 '21

Ok I'll try this thank you

3

u/bleuge Nov 09 '21

var

c1:integer;

myarray:array[1..7] of integer;

//arrays are just as a variable, but they store more than 1 value,

//accessing value n is just using array[n]

begin

for c1:=1 to 7 do

myarray[c1]:=random(1000); //get random from 0 to 999

//now we are out of the loop, retrieve and print the values

for c1:1 to 7 do

writeln(myarray[c1]);

//easy ;)

end.

1

u/[deleted] Nov 09 '21

Thanks man you're a life saver

1

u/[deleted] Nov 08 '21

Sorry for this i have to figure out all my work by my self and mot sure how all the functions work

2

u/Onceandfuturekingz Nov 08 '21

What language even

4

u/drwtsn32 Delphi := v2010 Nov 08 '21

We are in the Delphi subreddit...soooo.....lol

1

u/gman6528 Nov 08 '21

for i := 1 to 7 do

begin

myInt := 1 + Random(100);

SomeRoutine(myInt);

end;