Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 26th, 2005, 05:33 PM
seismicmike seismicmike is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 3 seismicmike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 17 sec
Reputation Power: 0
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????

Reply With Quote
  #2  
Old October 26th, 2005, 06:14 PM
seismicmike seismicmike is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 3 seismicmike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 17 sec
Reputation Power: 0
Talking

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

Reply With Quote
  #3  
Old October 26th, 2005, 06:16 PM
seismicmike seismicmike is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 3 seismicmike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 17 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Random Numbers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT