I need some help.

I need to generate a random number 1-59, 1000+ times. Then I need the application to tell me which number(s) were randomly selected the most. I have literally tried everything I can think of and cant find a decent way to do this. Anybody want to help me out? Thanks.

-Chandler Mayo [import]uid: 104852 topic_id: 25708 reply_id: 325708[/import]

  
frequency = {}  
for i = 1, 1000 do  
 frequency[i] = 0  
end  
  
for i = 1, 1000 do  
 local val = random(59)  
 frequency[val] = frequency[val] + 1  
end  
  
table.sort(frequency, function(a,b) return a\>b end)  
  
for i = 1, 10 do  
 print(i,frequency[i])  
end  
  

That will print the top 10 most popular numbers
[import]uid: 19626 topic_id: 25708 reply_id: 103926[/import]

To be honest I didn’t even think of doing it like that. I owe you one. [import]uid: 104852 topic_id: 25708 reply_id: 103928[/import]

Unfortunately now that I have had some time to test it some things aren’t working right. I keep getting this over and over:

1 26
2 24
3 24
4 24
5 23
6 23
7 22
8 22
9 22
10 21

The top ten random numbers are always 20 - 29. I tried making some modifications to it but I cant get it to work. [import]uid: 104852 topic_id: 25708 reply_id: 103932[/import]

are you seeding the random number generator? If you don’t you will get the same sequence of random numbers.

Random numbers are not really random, they are called Pseudo Random numbers. The function needs a starting point or a “seed”. If the seed is the same each run, you get a repeatable set of numbers.

Add this at the top of your program:

math.randomseed(os.time())

[import]uid: 19626 topic_id: 25708 reply_id: 103933[/import]

That was one of the things I tried, same problem. [import]uid: 104852 topic_id: 25708 reply_id: 103935[/import]

@chandler767 please don’t take this as a snotty/snide remark but:

What do you expect the results to be?

Over 1000 random numbers in the range of 1-59 its going to flatten out quite a bit. There should be an average of 17 per entry. (1000 / 59 = 16.9491525). Because it is random, there will be some variation. The higher the sample set, say 1,000,000 iterations, the variance will drop considerably.

At 1 million iterations, I should average 16,949 per number and sure enough, I get a 16949.1525 average. But the variance between #1 and #10 range from 17227 - 17102. The larger the sample size the closer each of the 59 values will be to the expected average.

With a sample size of 100, you get considerably more variance. I still averaged 1.69491524 (my sample size was 10% of 1000, so my average will be 10% of the 1000 sample), but my variance ran from 7 to 3 in the top 10. A variance of 7 to 3 seems less than 29 - 22, but it’s not, with the sample size being smaller there will be more variation.

What you want to do to see what I think you want to see is to remove the sort and then print out all 59 numbers. Then you will see spikes at some numbers and valleys in others. By sorting them to produce the top hits, your only going to get the most popular numbers and when run over that sample size is going to be pretty consistent.

That’s the way random numbers work.
It’s the way random distribution works. [import]uid: 19626 topic_id: 25708 reply_id: 103938[/import]

@chandler

Rob’s code shows the frequency of the top 10 numbers. Those numbers are not the actual number that is generated, just the count of how many times they are selected. Could that be the issue? [import]uid: 31262 topic_id: 25708 reply_id: 103943[/import]

>>Those numbers are not the actual number that is generated, just the count of how many times they are selected. Could that be the issue?<<

Yes it is.
The left column will always say 1…10
The right column may vary a little bit… perhaps the top one might be 30, maybe 20, but there won’t be much variation.

For better visualisation, why not make a quick mini-bar chart?

Don’t sort the results.

for each of the 59 values (for x =1,59,do…) , add a filled rectangle to the display

x*3 ,0, frequency[x] * 8,2

that will get you something like this:




--------------------------------- [import]uid: 108660 topic_id: 25708 reply_id: 103946[/import]

aaaron is right. The table.sort function alters the table as it sorts. Rob’s frequency table is structured with an index from 1 - 1000 even though only 59 of these are needed. The values of index 1-59 are incremented by one for each time in 1000 tries that index # comes up randomly. So far so good. But the *number that was picked* is the table index, not the value, and this index is destroyed when the table is sorted. [lua]frequency[1][/lua] will always have the largest value but it’s forgotten its original index, which is now “1”.

Compare to this code, which shows this happening under the hood:

[lua]frequency = {}
for i=1,59 do
frequency[i] = { i, 0 }
end

math.randomseed( os.time() )

for i=1,1000 do
local val = math.random(1,59)
frequency[val][2] = frequency[val][2] + 1
end

table.sort(frequency, function(a,b) return a[2]>b[2] end)

for i=1, 59 do
print( “newindex=”…i, “oldindex=” … frequency[i][1], “frequency=”…frequency[i][2] )
end[/lua] [import]uid: 44647 topic_id: 25708 reply_id: 103948[/import]

Yes, I needed the number that was randomly selected the most. For example I have 5, 7, 5, 3, 4, 8, randomly picked. I need the program to tell me which one was randomly selected the most often which in the example it is 5 because it was picked twice.

@toby2 Your values in old index appear to be what I needed. Thanks. [import]uid: 104852 topic_id: 25708 reply_id: 103990[/import]