delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/16/07:31:51

From: "A. Sinan Unur" <sinan DOT unur AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Easy question...
Date: Mon, 16 Mar 1998 06:55:54 -0500
Organization: Cornell University (http://www.cornell.edu/)
Lines: 27
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <350D1349.53C8C4E6@cornell.edu>
References: <19980315154000 DOT KAA08561 AT ladder03 DOT news DOT aol DOT com> <#zHZsoHU9GA DOT 191 AT upnetnews02 DOT moswest DOT msn DOT net>
NNTP-Posting-Host: cu-dialup-1227.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

DeHackEd wrote:
> 
> rand() and random() take no arguements. They return numbers from 0 to 
> RAND_MAX or INT_MAX (not sure which). If you want to limit what they 
> return, use a mod (rand()%10)command. If you want either 0 or 1, use 
> random()&0x400 for safety, as random generators are not as random as 
> one would hope.

you don't necessarily have to worry about this if you are just writing a
game, but a more proper way of generating a bernoulli (success/fail,
coin toss) random variable with probability of success p would be to use

#include <stdlib.h>

int bernoulli(double p)
{
  return rand() > p*RAND_MAX;
}

-- Sinan
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA

mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/

- Raw text -


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