delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/29/09:08:21

Message-ID: <3892AF0D.2603ECDB@home.comNOSPAM>
From: Robin Johnson <NOSPAMrobbat2 AT home DOT comNOSPAM>
Organization: Orbit Computers
X-Mailer: Mozilla 4.5 [en]C-AtHome0405 (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random numbers with rand() and srand()
References: <s9559v52q0s107 AT corp DOT supernews DOT com>
Lines: 55
Date: Sat, 29 Jan 2000 09:12:51 GMT
NNTP-Posting-Host: 24.113.36.103
X-Complaints-To: abuse AT home DOT net
X-Trace: news1.rdc1.bc.home.com 949137171 24.113.36.103 (Sat, 29 Jan 2000 01:12:51 PST)
NNTP-Posting-Date: Sat, 29 Jan 2000 01:12:51 PST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"William J. Urban II" wrote:

> Hello all.  Well I am trying to generate some random "dice" rolls with rand
> and I am having difficulty.  I have #include <time.h> so that I can use
> srand(time(NULL)); to generate a new seed each time but the problem is when
> I roll a d6 for example it rolls it and will keep generating the same number
> each time the program is run.  Here is what I am talking about:
>
> #include <time.h>
>
> ~~~skipping main and stuff~~~
> int die;
>
> srand(time(NULL)); got this out of my c++ book to use the seconds to
> generate a "seed"
>
> die = ((rand%6)+1);
> die = ((rand%6)+1);
> die = ((rand%6)+1);
> die = ((rand%6)+1);
>
> when I run this each time i run it I get a different number but all four of
> them are the same.  For example...
> first run
> die = 2
> die = 2
> die = 2
> die = 2
>
> second run
> die = 5
> die = 5
> die = 5
> die = 5
>
> Could anyone help me generate more random numbers in my code?  Thanks in
> advance.
>
> Will

The problem with rand and srand is that they are not that random if the modulus
value is small,
try using random() instead


--
Robin Hugh Johnson
"Robbat2"
QTOD:"I used to be an idealist, but I got mugged by reality."
E-Mail     : robbat2 AT home DOT com
ICQ#       : 30269588 or 41961639
Home Page  : http://robbat2.cjb.net
Time Zone  : Pacific Daylight (GMT - 8)


- Raw text -


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