| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: c++ help |
From: "Geo."
well a simple solution to that would be
int n = (rand()/RAND_MAX)*99999
That should give me a pretty random distribution of the right most 4
characters, theoretically with no bias.
Geo.(there actually is a bias dependent on the time you run the program)
"Paul Ranson" wrote in message
news:41b58a1f{at}w3.nls.net...
> It depends how random you want your results to be. 'rand' returns what
> should be an even distribution between 0 and 32767, so a simple '%10' has
a
> slight bias away from 8 and 9, if you look at the second and third more
> significant digits then they will have a bias towards 0 and also away from
> 7, 8, 9. If 'rand' returns 8 you will get 0, 0, 8 etc.
>
> Paul
>
> "Geo" wrote in message
news:41b4ecad$1{at}w3.nls.net...
> >> Another point, probably unimportant. You could extract the three
> >> digits that you want from one call to rand, as below:
> >> int n = rand();
> >> itoa((n%10),a,10);
> >> itoa(((n/10)%10),b,10);
> >> itoa(((n/100)%10),c,10);
> >
> > See? Now that's the kind of stuff they never show you in a programming
> > book.
> > Now I'm going to have to do a whole new program just to play around with
> > this and see exactly what it's doing so I can build my mental image.
> > That's really slick, it allows me to extract just one
> > digit
> > out of a number so if n=4581 then c ends up being the 5. Very handy
> > technique, that was probably more useful than learning about itoa.
> >
> > Geo.
> >
> >
>
>
--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)SEEN-BY: 633/267 270 5030/786 @PATH: 379/45 1 396/45 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.