May 16, 2008

Random Numbers

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Assembler and Delphi

free web hosting

Random Numbers

kvarnerexpress
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:

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

 

 

 


Reply

bureX
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.

Reply

dul
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]

Reply

alphaobject
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



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : random numbers


    Looking for random, numbers

Searching Video's for random, numbers
advertisement



Random Numbers



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE