math - How to transform any 64-bit integer to a uniformly distributed random number in certain interval -
can transform integers between [long.min_value, long.max_value] [0,1] uniformly , randomly? main purpose can pick fraction, e.g. 0.1 , select uniformly , randomly distributed 10% of longs. if transformation method f(x)
, can check f(x)< 0.1
on long see if belongs 10% selected numbers. important requirements:
- it can controlled seeds, can same results every time (when want), , change seed different result.
- when increase fraction x y, want selected numbers in x selected in y. example, if 894230 selected when pick 10% of numbers, should selected when pick 20% numbers.
i can select seeds won't result in obvious patterns such
double f(x) { return (x%1000)/1000.0; }
.nice have (but not necessary):
- i can use dummy seed result in obvious pattern (so looks obvious in unit test).
i use java, don't mind answers in language can rewritten in java
if job identify whether x
belongs set of of chosen integers, believe following achieve of first 3 requirements:
(long)anyreasonablehashfunction(x ^ seed) < long.max_value * fraction
Comments
Post a Comment