Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: easy question about random numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53392] Re: [mg53382] easy question about random numbers
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Sun, 9 Jan 2005 23:03:46 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

li2={1/6,1/6,1/6,1/6,9/60,11/60};

li3=FoldList[Plus,0,li2];

drng[] := Module[{x=Random[]},Count[li3,_?(#<x&)]];

data=Table[drng[],{10000}];

(Count[data,#]&/@Range[6])/Length[data]//N

{0.1609,0.1703,0.1653,0.1659,0.1511,0.1865}

(Plus@@%)==1

True

Round[60*#]/60&/@%%

{1/6, 1/6, 1/6, 1/6, 3/20, 11/60}


Bob Hanlon

> 
> From: Pedrito <pedrito6 at softhome.net>
To: mathgroup at smc.vnet.net
> Date: 2005/01/08 Sat PM 11:02:44 EST
> To: mathgroup at smc.vnet.net
> Subject: [mg53392] [mg53382] easy question about random numbers
> 
> Hi everybody!
> 
> I wanted to obtain a discrete random number generator that I needed for
> a project.
> 
> On the library Statistics`DiscreteDistributions` I could find the 
DiscreteUniformDistribution
> function. But I wanted to specify the probability for each one of the
> states.
> 
> 
> For instance:
> If we need to simulate an unfair dice, we could have this probabilities
> for each one of the sides:
> {1/6, 1/6, 1/6, 1/6, 9/60, 11/60}
> 
> So I wrote:
> li2 = {1/6, 1/6, 1/6, 1/6, 9/60, 11/60}
> li3=FoldList[Plus,0,li2]
> Module[{i = 1, r = Random[]}, While[ !li3[[i]] < r < li3[[i + 1]], i++]; i]
> 
> It works ok but I don't know if there is another (better) way of doing
> this.
> Any suggestion?
> 
> 


  • Prev by Date: Re: easy question about random numbers
  • Next by Date: Re: Function Show[]
  • Previous by thread: Re: easy question about random numbers
  • Next by thread: Re: easy question about random numbers