Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: Testing a random integer generator
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Bill Rowe

Posts: 1,647
Registered: 3/14/08
Re: Testing a random integer generator
Posted: Apr 8, 2013 12:05 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 4/6/13 at 9:53 PM, c_mcinnis@hotmail.com (Clif McInnis) wrote:

>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.


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

The line above is the reason the code never prints a value. It
does not set x,p or q to 0. Look up RuleDelayed in the
documentation center. Change this to

x=0;

and your While loop will print values

>While[x < 100, {
>Clear[p, q],


the line above accomplishes nothing. It is not necessary in
Mathematica to clear variables before using them.

>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++]


Note a better choice when you simply want to execute a section
of code n times is Do rather than While. The code below:


Do[
{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]}, {100}]

does exactly the same as your code without the need for x.

Also, Print sends its output to $Output. So, unless you redefine
$Output, you won't capture the results of your generator for
further analysis.

an efficient way to capture the output would be as follows:


output = {};
Do[
{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}]]}],
output = {output, p, q}}, {100}]

This captures all of the p,q values in a nested list which can
be made into a simple list using Flatten. That is you can do

Histogram[Flatten@output]

to create a histogram of p,q values. Or

Mean[Flatten@output] to compute the mean value and so on.





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.