Jump to content
xisto Community
kvarnerexpress

Random Numbers

Recommended Posts

I'm trying to do random numbers...

I use Randomize() ;
and then run Random() ; several times in a loop

the only trouble is that each time I run the program the output is the same each pass through the loop..

ie... it's supposed to output a random number on each of three passes through the loop...

it's actuall..

Code:

function RandomFunction(): Integer;var    x: Integerbegin    x := Random(12) + 1;    Result := x;end;procedure Main;var   i: Integer;begin   Randomize();   for i := 1 to 3 do      showmessage(IntToStr(CallMyFunction()));end;



so... it'll output 4 three times, and then i'll run it again and it'll output 7 three times... grrrrr

i'm pretty sure i remember something about the Randomize() function being linked to the system clock, but I don't remember how... or necessarily all the implications of it... though I do remember that I'm not supposed to call it in the loop...
thanks,kvarnerexpress

Share this post


Link to post
Share on other sites

Well, the only errors that I found in your code were a missing semicolon... I replaced "CallMyFunction()" with "RandomFunction()", added the missing semicolon and compiled the program... Everything works! I don't really see what could be wrong with your code... Try doing some debugging and watch the "X" variable change during the execution of the program.

Share this post


Link to post
Share on other sites

Of course, that is doesh't work. You didn't send any random numbers. Because you calling unidentified function which hash't got in your source code or calling wrong function. try this one. It will work

[quote=

showmessage(IntToStr(RandomFunction));

Share this post


Link to post
Share on other sites

Can try this version : function RandomFunction: Integer;var x: Integer;begin Randomize; x := Random(12) + 1; Result := x;end;procedure Main;var i: Integer;begin for i := 1 to 3 do showmessage(IntToStr(CallMyFunction()));end;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.