delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/19/13:24:49

Message-ID: <37E5097E.1AF629C1@pmail.net>
Date: Sun, 19 Sep 1999 18:04:15 +0200
From: Fred Backman <fredrick DOT backman AT pmail DOT net>
X-Mailer: Mozilla 4.6 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Random number
References: <37E3E460 DOT E60AAC43 AT pepparkakor DOT demon DOT nl> <7s1fds$4s72$1 AT newssvr03-int DOT news DOT prodigy DOT com> <37E4895C DOT 2ECF45FA AT pepparkakor DOT demon DOT nl> <37E4E4D9 DOT 7408157 AT pepparkakor DOT demon DOT nl>
Reply-To: djgpp AT delorie DOT com

I think you're giving the random number generator the same seed for all values of
C. Try moving "Number random" outside the loop.

Roger wrote:

> But it is the same number each time!!! :-(
> I want different numbers.
>
> Roger wrote:
>
> > Thanks Winston,it works,and so easy.  :-)
> >
> > Winston Kotzan wrote:
> >
> > > The way I use to create random numbers is using the rand() function.  The
> > > syntax is:
> > >
> > > rand() % number
> > >
> > > You can use a variable to catch the generated number:
> > >
> > > variable = rand() % number
> > >
> > > The value of the generated number (variable), will be 1 less than number.
> > >
> > > Roger <roger AT pepparkakor DOT demon DOT nl> wrote in message
> > > news:37E3E460 DOT E60AAC43 AT pepparkakor DOT demon DOT nl...
> > > > Hi,everyone.
> > > >
> > > > I have a problem with random number.I try this:
> > > >
> > > > First I make a class:
> > > >
> > > > class Number
> > > > {
> > > > public:
> > > > Number() {srandom((unsigned)time(0));}
> > > > Number(unsigned seed) {srandom(seed);}
> > > > int MyNumber(int min,int max)
> > > > {
> > > > if (max<0)max=0;
> > > > if((min==max) && (min=0))return 0;
> > > > return random()%(max-min+1)+min;
> > > > }
> > > > };
> > > >
> > > > then in my program i do a loop,like this:
> > > >
> > > > for(p=1;p<10;++p)
> > > > {
> > > > Number random;
> > > > C[p]=random.MyNumber(0,100);
> > > > }
> > > >
> > > > And I got every time same number on all C's!!
> > > >
> > > > Why? I have read everything on the net and in my book,and i can't solve
> > > > it.
> > > >
> > > > Thanks for a wonderful list
> > > >
> > > >
> > > >
> > > >

- Raw text -


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