| delorie.com/archives/browse.cgi | search |
| From: | "Winston Kotzan" <WINZ AT prodigy DOT net> |
| Newsgroups: | comp.os.msdos.djgpp |
| Subject: | Re: Random number |
| Date: | Sat, 18 Sep 1999 20:51:03 -0500 |
| Organization: | Prodigy Internet http://www.prodigy.com |
| Lines: | 52 |
| Message-ID: | <7s1fds$4s72$1@newssvr03-int.news.prodigy.com> |
| References: | <37E3E460 DOT E60AAC43 AT pepparkakor DOT demon DOT nl> |
| NNTP-Posting-Host: | chcgb307-15.splitrock.net |
| X-Trace: | newssvr03-int.news.prodigy.com 937705724 3351044 209.156.14.245 (19 Sep 1999 01:48:44 GMT) |
| X-Complaints-To: | abuse AT prodigy DOT net |
| NNTP-Posting-Date: | 19 Sep 1999 01:48:44 GMT |
| X-Newsreader: | Microsoft Outlook Express 5.00.2314.1300 |
| X-MSMail-Priority: | Normal |
| X-Priority: | 3 |
| X-MimeOLE: | Produced By Microsoft MimeOLE V5.00.2314.1300 |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
| Reply-To: | djgpp AT delorie DOT com |
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
>
>
>
>
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |