I wrote a litte function in Javascript that displays a random picture. The actual line returning the pics number looks like this:
num = Math.floor(Math.random() * RNDGALSIZE);
with RNDGALSIZE currently = 72.
however, i felt that some pictures are hit quite often while others didnt come for quite a time, so I wrote a loop to generate num
some number of times, and record how many times each value of num
appeared. Here is the result:
2, 1, 2, 1, 1, 3, 1, 2, 2, 2, // num = 0 to 9
2, 1, 0, 1, 0, 2, 2, 1, 0, 1, // num = 10 to 19
1, 2, 0, 1, 0, 0, 1, 1, 2, 1, // num = 20 to 29
1, 0, 1, 2, 0, 2, 1, 0, 0, 0, // num = 30 to 39
2, 2, 3, 3, 0, 1, 1, 1, 0, 3, // num = 40 to 49
2, 1, 2, 1, 3, 2, 3, 2, 1, 1, // num = 50 to 59
2, 1, 1, 0, 0, 2, 2, 1, 0, 1, // num = 60 to 69
3, 1 // num = 70 and 71
As you can see, no value appeared more than 3 times, and sixteen values did not occur at all. While some values might not occur, i think 16 is way to much. Is there anything wrong with my aproach?
Update:
A little Later:
4, 4, 3, 6, 5, 5, 3, 3, 2, 2,
2, 2, 2, 2, 3, 5, 3, 1, 4, 2,
4, 3, 0, 1, 1, 0, 1, 4, 4, 4,
2, 0, 5, 3, 0, 4, 2, 0, 2, 1,
2, 3, 4, 3, 2, 4, 3, 2, 0, 5,
4, 4, 4, 2, 3, 4, 4, 4, 5, 1,
2, 2, 4, 2, 0, 3, 4, 2, 2, 1,
4, 1,
As you can see, while the 3 is hit 6 times, there still 7 zeroes in the array :/
So, solution would be to keep track of how often a number was hit and in case the random-generator hits a number, that has been hit already to often, provide a number that has been ignored to often…
var minhit = Number.MAX_VALUE;
var maxhit = 1;
var index_min = 0;
for (i=0; i<RNDGALSIZE; i++)
{
if (imgnum[i] < minhit)
{
minhit = imgnum[i];
index_min = i;
}
else if (imgnum[i]>maxhit)
{
maxhit = imgnum[i];
}
}
var num = Math.floor(Math.random() * RNDGALSIZE);
if (imgnum[num] == maxhit)
{
num = index_min;
}
imgnum[num]++;
Result:
2, 2, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 1, 1,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 2, 1,
1, 1, 1, 1, 1, 1, 2, 1, 1, 1,
1, 1,
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments