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

From: "William J. Urban II" <wjurban AT tca DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Random numbers with rand() and srand()
Date: Sat, 29 Jan 2000 01:20:30 -0600
Organization: Posted via Supernews, http://www.supernews.com
Lines: 41
Message-ID: <s9559v52q0s107@corp.supernews.com>
X-Complaints-To: newsabuse AT supernews DOT com
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2919.6600
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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




- Raw text -


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