Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14095 > unrolled thread
| Started by | Clif McInnis <c_mcinnis@hotmail.com> |
|---|---|
| First post | 2013-04-07 01:55 +0000 |
| Last post | 2013-04-09 08:04 +0000 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Testing a random integer generator Clif McInnis <c_mcinnis@hotmail.com> - 2013-04-07 01:55 +0000
Re: Testing a random integer generator Peter Pein <petsie@dordos.net> - 2013-04-08 04:06 +0000
Re: Testing a random integer generator (Answered) Clif McInnis <c_mcinnis@hotmail.com> - 2013-04-08 04:05 +0000
Re: Testing a random integer generator Peter Pein <petsie@dordos.net> - 2013-04-08 04:06 +0000
Re: Testing a random integer generator Clif McInnis <c_mcinnis@hotmail.com> - 2013-04-09 08:04 +0000
Re: Testing a random integer generator Ray Koopman <koopman@sfu.ca> - 2013-04-09 08:04 +0000
| From | Clif McInnis <c_mcinnis@hotmail.com> |
|---|---|
| Date | 2013-04-07 01:55 +0000 |
| Subject | Testing a random integer generator |
| Message-ID | <kjqjln$37o$1@smc.vnet.net> |
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})
While[x < 100, {
Clear[p, q],
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++]
[toc] | [next] | [standalone]
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Date | 2013-04-08 04:06 +0000 |
| Message-ID | <kjtfng$du3$1@smc.vnet.net> |
| In reply to | #14095 |
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
[toc] | [prev] | [next] | [standalone]
| From | Clif McInnis <c_mcinnis@hotmail.com> |
|---|---|
| Date | 2013-04-08 04:05 +0000 |
| Subject | Re: Testing a random integer generator (Answered) |
| Message-ID | <kjtfmq$dtl$1@smc.vnet.net> |
| In reply to | #14095 |
I had gotten a message that the post had taken too long to upload, so I thought that it was not here. I found a way to make the "test" work and got the results that I expected. In the event that anyone else would be interest
ed the code that I used is as follows:
Peace -- Clif
Initialization /.
Initialization :> {x = 0, p = 0, q = 0, mne = 0, mnn = 0, lte = 0,
ltn = 0}
While[x < 100000, {
Clear[p, q],
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," and ",
q],*)
If [p >
9, {If[p == q, mne += 1, mnn += 1]}, {If[p == q, lte += 1,
ltn += 1]}]}; x++]; Print[mne/x, Space[10], mnn/x, Space[10],
lte/x, Space[10], ltn/x
]
[toc] | [prev] | [next] | [standalone]
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Date | 2013-04-08 04:06 +0000 |
| Message-ID | <kjtfnm$du5$1@smc.vnet.net> |
| In reply to | #14095 |
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})
> While[x < 100, {
> Clear[p, q],
> 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++]
>
I've got another one that generates the pairs very fast:
With[{disttbl =
MapAt[TransformedDistribution[{x, x}, x \[Distributed] #] &,
DiscreteUniformDistribution /@
Flatten[Table[
Flatten[Table[d, {d, {{1, 9}, {10, 20}}}, {n}], 2 - n],
{n, 2}], 1],
{{1}, {2}}]},
thedist = MixtureDistribution[{1, 3, 1, 3}, disttbl]];
rndPairs[n_] := Round@RandomVariate[thedist, n];
the "Round" is necessary because TransformedDistribution returns inexact
numbers (bug or feature?).
Have fun,
Peter
[toc] | [prev] | [next] | [standalone]
| From | Clif McInnis <c_mcinnis@hotmail.com> |
|---|---|
| Date | 2013-04-09 08:04 +0000 |
| Message-ID | <kk0i38$2tn$1@smc.vnet.net> |
| In reply to | #14095 |
Peter and Bill, Thank you very much for your answers, I am going to run your codes and look into the functions that I used unnecessarily and/or incorrectly. Hope that you both will have a good day. Clifton McInnis
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2013-04-09 08:04 +0000 |
| Message-ID | <kk0i33$2tl$1@smc.vnet.net> |
| In reply to | #14095 |
On Apr 6, 6:55 pm, Clif McInnis <c_mcin...@hotmail.com> 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.
>
> Clif McInnis
>
> Initialization :> ({x = 0, p = 0, q = 0})
> While[x < 100, {
> Clear[p, q],
> 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++]
randompair[range_] := {#, If[RandomInteger[] == 0,
#, RandomInteger[range]]} & @ RandomInteger[range]
Table[randompair@If[RandomInteger[3] == 0, {1, 9}, {10, 20}], {100}]
{{14, 14}, {14, 15}, {12, 12}, {5, 1}, {5, 5}, {17, 17}, {18, 11},
{13, 13}, {20, 11}, {15, 15}, {11, 19}, {6, 6}, {14, 20}, {5, 9},
{14, 12}, {7, 7}, {15, 15}, {13, 17}, {13, 17}, {10, 10}, {20, 17},
{10, 17}, {17, 17}, {17, 17}, {9, 9}, {8, 6}, {15, 13}, {18, 18},
{3, 3}, {18, 12}, {13, 19}, {18, 18}, {8, 3}, {5, 6}, {5, 5},
{13, 16}, {20, 20}, {18, 18}, {20, 20}, {14, 14}, {10, 16},
{17, 12}, {17, 18}, {18, 20}, {18, 16}, {14, 14}, {20, 14},
{19, 19}, {2, 2}, {20, 17}, {10, 15}, {17, 17}, {5, 5}, {7, 3},
{4, 4}, {16, 16}, {16, 18}, {3, 5}, {7, 7}, {20, 20}, {13, 13},
{18, 16}, {13, 13}, {7, 7}, {11, 13}, {17, 17}, {8, 8}, {17, 17},
{19, 17}, {19, 14}, {19, 14}, {19, 19}, {19, 19}, {19, 19},
{14, 14}, {6, 8}, {8, 8}, {1, 1}, {15, 17}, {15, 15}, {18, 18},
{20, 17}, {10, 10}, {16, 15}, {10, 13}, {4, 4}, {3, 5}, {20, 20},
{8, 6}, {20, 13}, {17, 14}, {20, 20}, {11, 11}, {16, 19},
{20, 13}, {16, 20}, {13, 13}, {10, 17}, {3, 4}, {18, 19}}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web