TIP: Click on subject to list as thread! ANSI
echo: nthelp
to: Geo
from: Paul Ranson
date: 2004-12-09 12:33:34
subject: Re: c++ help

From: "Paul Ranson" 

In general rand()/RAND_MAX is less than one, and you're working with
integers so it ends up as 0.

If you write it as 'rand() * 9999 / RAND_MAX' then it will work better, but
I'd need to sit down and think about it from a random, or even getting
every value, pov.

BTW needing to think about this is nothing to do with C++ per se, it's just
what happens when you start investigating cutting edge stuff like random
numbers. But it's got to be good to be thinking about whether what you
actually mean is what you've coded. I'm thinking of exploding Ariane
rockets now.

Paul

"Geo"  wrote in message
news:41b82ee0$1{at}w3.nls.net...
> Why would it return 0? Do I need another set of () around the whole thing?
>
> I see what you are saying about the bias if you use a straight rand()
> function but when you divide by RAND_MAX then rand() gives a value between
> 0
> and 1 which should have an even distribution, no?
>
> int n = (rand()/32767)*9999
>
> should give you an even distribution of numbers from 1 to 9999. I guess I
> could write a program that loops and tracks the distribution to see,
> something fun to play with this weekend.
>
> 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™.