www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/21/04:17:58

Date: Thu, 21 Nov 1996 10:03:34 GMT
From: kvhk AT ivs4 DOT barco DOT com (Koen Van Herck)
Message-Id: <9611211003.AA01758@ivs4.>
To: djgpp AT delorie DOT com
Subject: Proper use of rand()
Reply-To: Koen DOT VanHerck AT barco DOT com

It seems that rand() and random() always return the same numbers when
a program is started. 

I fixed this by setting the seed to a number depending on the clock: 

unsigned long u;
u = time(0);
u = u ^ (u << 16);
srand(u); 

You need to wait at least 1 second between different runs to get other
random numbers.

It would be better to use a function that changes more often than
time(), but I didn't find one.

----
Koen Van Herck
Electronic Design Engineer
E-mail: Koen DOT VanHerck AT barco DOT com

 BBBB    AAA   RRRR    CCC   OOO     B A R C O   V I S U A L   S Y S T E M S
 B   B  A   A  R   R  C     O   O    A division of  Barco Projection Systems
 B BB   A AAA  R RR  C    OO  O  OO   
 B   B  A   A  R   R  C     O   O    Noordlaan 5      Tel +32 (0)56 36 85 71
 BBBB   A   A  R   R   CCC   OOO     B-8520 Kuurne    Fax +32 (0)56 36 83 55

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019