Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Random Numbers


3 replies to this topic

#1 kvarnerexpress

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 407 posts

Posted 27 October 2005 - 09:01 PM

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: Integer
begin
    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

#2 bureX

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 318 posts

Posted 27 October 2005 - 09:24 PM

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.

#3 dul

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 73 posts

Posted 27 October 2005 - 11:26 PM

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));
[/quote]

#4 alphaobject

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 15 January 2006 - 08:02 AM

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;




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users