I am searching for a specific number (some specific numbers) ... later: magicalNumber
Hi there, I need an algorithm which always gives the same distribution of objects into (3) buckets regarding a given seed on a random generator. As far as I understood ..
Random generator = new Random (givenSeed);
.. always gives the same result (that far so good).
Assumption: Small changes in the givenSeed change the random output of the generator only slightly (caution - its not continuous)
Random for seed: '399197703593 + 0' ---> 0.7112663450468925
Random for seed: '399197703593 + 5' ---> 0.7111767781758487
Random for seed: '399197703593 + 10' ---> 0.7135954711242217
Random for seed: '399197703593 + 15' ---> 0.712789235174377
Random for seed: '399197703593 + 20' ---> 0.7123413263133521
Random for seed: '399197703593 + 25' ---> 0.7093851427317492
Random for seed: '399197703593 + 30' ---> 0.7089372338707244
Random for seed: '399197703593 + 35' ---> 0.7081310128220408
.. Never mind ..
Lets assume we have 100 Objects to put into (3) buckets.
The chance for an object to be put into one of the (3) buckets are not equally distributed (e.g. 50%; 30%, 20%). Therefore to have a good distribution after 100 cycles we need a magical number to "jump over" the Java Generator's seed iteration circles.
First idea would be:
generator = new Random (givenSeed + objectNumber * magicalNumber);
The objectNumber is the iteration. The magical number needs to be chosen to not be a multiple of the generators circle size (circle size = almost same number after offset). Probaply a prime-number would be nice - but not necessarily needed. Nevertheless, the magical number needs to hit the 0-1 range in a "good distribution" for 100 / 1.000 / 10.000 objects (caution - different amounts of objects could lead to a changed best magical number).
I hope I did not get too much wrong in the basics.
Thanks for all thoughts coming up ^.^
-------------------------------------------------
Quick escape In case you have that problem, but the magical number-riddle is not yet solved: You could choose any bigger number and rely on the randomness of later digits.
If in the output the first 6 digits are comparably stable (here: (0.)712341) AND later are not ..
Random for seed: '399197703593 + 20' ---> 0.7123413263133521
.. you could take the later ones. They are still dependent on your seed (good for automated testing purposes), but AFAIK meet the upper riddle-requirements.
double resultFromGenerator = 0.7123413263133521; // Not random due to seed
double afterMod = (resultFromGenerator * 100000000) % 100; // Cutting 'stable' front digits
double finalNumber = afterMod/100; // (Pseudo) random number in range 0 <= x <= 1
This would output:
resultFromGenerator: 0.7123413263133521
afterMod: 32.6313352137804
finalNumber: 0.32631335213780405
Again - I hope I did not get too much wrong here. :)
Aucun commentaire:
Enregistrer un commentaire