|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Random Numbers
Hey hey...
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... help???? |
|
#2
|
|||
|
|||
|
HAHAHA!!! Never mind!!! lol
it was actually more like this!!! Code:
// Generate Date generates a random day
function GenerateDate(): string ;
var
ranMonth, ranYear, ranDay: integer ;
month, year, day: string ;
begin
// Get random month
ranMonth := Random(12) + 1 ;
// validate
If ranMonth < 1 then
begin
ranMonth := 1 ;
end ;
If ranMonth > 12 then
begin
ranMonth := 12 ;
end ;
// Get random year
ranYear := Random(15) + 1990 ;
// validate
If ranYear < 1990 then
ranYear := 1990 ;
If ranYear > 2005 then
ranYear := 2005 ;
if (ranMonth = 2) then
// february
begin
if((ranYear Mod 4) = 0) then
// leap year
ranDay := Random(29) + 1
else
// not leap year
ranDay := Random(28) + 1 ;
end
else if ((ranMonth = 1) Or (ranMonth = 3) Or (ranMonth = 5) Or
(ranMonth = 7) Or (ranMonth = 8) Or (ranMonth = 10) Or
(ranMonth = 12)) then
// month with 31 days
ranDay := Random(31) + 1
else
ranDay := Random(30) + 1 ;
// format for output
if ranMonth < 10 then
month := '0' + IntToStr(ranMonth)
else
month := IntToStr(ranMonth) ;
if ranDay < 10 then
day := '0' + IntToStr(ranDay)
else
day := IntToStr(ranDay) ;
year := IntToStr(ranYear) ;
Result := month + day + year ;
end ;
procedure Main ;
var
date: string ;
i: Integer ;
begin
Randomize ;
date = GenerateDate() ; // get a date to use later
for i:= 1 to 3 do
showmessage(r, date) ;
end ;
A pat on the back for anyone who spots my error... lol ok.. here it is... take notes all you beginning programmers... I build my apps very slowly checking to make sure that I don't make stupid errors as I go along... Therefore I make dummy variables and check the basics of user interface first, so that later when something goes wrong I know it's not that... soo.... I ran the whole thing without the loop first... i only needed to generate the date once, and I did it at the top to get it out of the way. Once I varified that the date was in fact being generated in the proper format, I was satisfied, and went on... BUT I FORGOT TO MOVE THE RANDOMIZER BACK INTO THE LOOP!!!!!!! at any rate... i fixed my own problem, it works like a charm now ![]() |
|
#3
|
|||
|
|||
|
btw... makest fun thou not if my code looks clunky or not really slick like most of you experienced delphi programmers...
i'm actually a rather accomplished java and c++ programmer, I'm still getting used to syntax in delphi... you guys are WEIRD!!! I WANT MY CURLY BRACES BACK!!!!! but don't worry, VB is weirder (though I have to shoot whoever came up with the idea of := being assignment and = being boolean) lol |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > Random Numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|