Message Boards Message Boards

GROUPS:

Iteration of increasing number of random numbers

Posted 12 days ago
136 Views
|
4 Replies
|
1 Total Likes
|

Consider the following code:

n = 10;
    t = Table[RandomInteger[], n];
    c = Counts[t]
    c1 = c[[1]] - n/2
    avg = c1/n // N

Basically I would like to produce a graph showing how increasing the number of trials causes the result to trend towards zero. I would like to iterate the above formulas over {n^i, {i,0,10}} and then graph the avg so the different number of trails can be compared, for n = 10, 100, 1000, etc. But I have a problem with c[[1]] takes the first position in the list and the first position is sometimes 0 and sometimes 1 and I would like to just look at "1".

4 Replies
Posted 12 days ago

So you are just interested in the number of times 1 appears in the list, right? There are several ways to do this. Here are a couple.

Reverse sort the counts:

c = Counts[t] // KeySortBy[Minus]

Filter out the zero's before counting:

t = Table[RandomInteger[], n] // Select[# > 0 &]
Posted 12 days ago

thanks Rohit, that is a start

n = 10;
t2 = Counts[Table[RandomInteger[], n] // Select[# > 0 &]];
(t2[[1]] - n/2)/n // N

now I would like to repeat this process to create a list where n increases, something like the following but it doesn't work

Table[
 t2 = Counts[Table[RandomInteger[], n] // Select[# > 0 &]];
 (t2[[1]] - n/2)/n // N, n = 10^i, {i, 0, 10}]
Posted 12 days ago

n needs to be defined before it is used.

Table[n = 10^i; 
 t2 = Counts[Table[RandomInteger[], n] // Select[# > 0 &]];
 (t2[[1]] - n/2)/n // N, {i, 1, 6}]

Starting i at 0 means there is a 50% chance that the count of 1 is 0 so Counts will return an empty Association and Part will fail. Also, ending it at 10 means the table will have 10^10 values.

Posted 12 days ago

Rohit, thank you very much for giving insight into my mistakes

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract