MathGroup Archive 2013

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

Search the Archive

Re: Testing a random integer generator

  • To: mathgroup at smc.vnet.net
  • Subject: [mg130395] Re: Testing a random integer generator
  • From: Peter Pein <petsie at dordos.net>
  • Date: Mon, 8 Apr 2013 00:06:16 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <kjqjln$37o$1@smc.vnet.net>

Am 07.04.2013 03:55, schrieb Clif McInnis:
> I am trying to test the random integer generator in a program to make sure
> that it gives the type of output that I am looking for. I expect it to generate (p<>q)<10 slightly less than 1/8 of the time and (p=q)<10 slightly more than 1/8 of the time, 9<(p<>q)<20 slightly less than 3/8 of the time, and 9<(p=q)<20 slightly more than 3/8 of the time. Regardless of my expectations I would like to see how the numbers actually come out, and to that end was attempting to test the test the code, but I can not get it to generate (print) any numbers. I am sure that I have made an error somewhere in the code, but I can not find it and am not getting any error messages. Thank you for any and all help.
>
> Clif McInnis
>
>


> Initialization :> ({x = 0, p = 0, q = 0})

This initializes nothing because of RuleDelayed ( :> ), but would be 
unusual too if Rule ( -> ) had been used.

> While[x < 100, {

As x is not used inside the loop, a Do[ ... , {100}] is sufficient.
If you want to use the generated pairs, the use of Table is the 
"natural" way to generate them in Mathematica.

>    Clear[p, q],

no need to clear, because the values are set in the next statement.
A Clear deletes the zero value from p too; it does not set the values of 
p and q to 0 -- be careful!

>    q = RandomInteger[{1, 4}], If[q < 4,
>     {p = RandomInteger[{10, 20}],
>      q = RandomInteger[],
>      If[q == 1, q = p, q = RandomInteger[{10, 20}]]}, {p = RandomInteger[{1, 9}],
>      q = RandomInteger[],
>      If[q == 1, q = p, q = RandomInteger[{1, 9}]]}], Print[p],
>    Print[q]}; x++]
>

randomPairs[number_] :=
  Table[
   Block[{integerrange =
      If[RandomInteger[3] == 0,
       {1, 9},
       {10, 20}]
     },
    If[RandomInteger[] == 0,
      ConstantArray, (*else*) Table][RandomInteger[integerrange], {2}]
    ], {number}]

To see if this does what you want, generate a few pairs

SeedRandom[1]; (* for reproducable rsults only *)
data = randomPairs[1000];

how many have got the same element twice?

Count[data, {p_, p_}]
--> 566

and how many have got the smaller values?

Count[data, {p_, _} /; p < 10]
--> 255

It seems to me that it works :)
Take a look at e.g.:

Histogram3D[Flatten[GatherBy[data, {SameQ @@ # &, #[[1]] < 10 &}], 1],
   ChartStyle -> {Darker[Green, .5], Darker[Red, .5], Green, Red}]


Peter



  • Prev by Date: Simplify an expression with respect to one of many variables
  • Next by Date: Re: Testing a random integer generator
  • Previous by thread: Re: Testing a random integer generator
  • Next by thread: Re: Testing a random integer generator