Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pick a random value between 0 and a bigint?

I have a combinatorics problem for which I want to be able to pick an integer at random between 0 and a big integer.


Inadequacies of my current approach

Now for regular integers I would usually write something like int rand 500; and be done with it.

But for big integers, it looks like rand isn't meant for this.

Using the following code, I ran a simulation of 2 million calls to rand $bigint:

$ perl -Mbigint -E 'say int rand 1230138339199329632554990773929330319360000000 for 1 .. 2e6' > rand.txt

The distribution of the resultant set is far from desirable:

  • 0 (56 counts)
  • magnitude 1e+040 (112 counts)
  • magnitude 1e+041 (1411 counts)
  • magnitude 1e+042 (14496 counts)
  • magnitude 1e+043 (146324 counts)
  • magnitude 1e+044 (1463824 counts)
  • magnitude 1e+045 (373777 counts)

So the process was never able to choose a number like 999, or 5e+020, which makes this approach unsuitable for what I want to do.

It looks like this has something to do with the arbitrary precision of rand, which never goes beyond 15 digits in the course of my testing:

$ perl -E 'printf "%.66g", rand'
0.307037353515625

How can I overcome this limitation?

My initial thought is that maybe there is a way to influence the precision of rand, but it feels like a band-aid to a much bigger problem (i.e. the inability of rand to handle big integers).

In any case, I'm hoping someone has walked down this path before and knows how to remedy the situation.

like image 523
Zaid Avatar asked Oct 10 '16 12:10

Zaid


People also ask

How do you limit math randomly?

random() method what generates a floating point random number from 0 to 1. In order to restrict it, you need to do something like this: var low:Number = 1; var high:Number= 100; var result:Number = Math.

How to get random value c#?

To generate random numbers in C#, use the Next(minValue, MaxValue) method. The parameters are used to set the minimum and maximum values. Next(100,200); We have set the above method under Random() object.


2 Answers

(Converted from my comment)

A more theoretical-driven approach would be using multiple calls to the PRNG to create enough random-bits for your number to sample. Care has to be taken, if the number of bits produced by some PRNG is not equal to the number of bits needed as outlined below!

Pseudocode

  • Calculate the bits needed to represent your number: n_needed_bits
  • Check the size of bits returned by your PRNG: n_bits_prng
  • Calculate the number of samples needed: needed_prng_samples = ceil(n_needed_bits / n_bits_prng)
  • While true:
    • Sample needed_prng_samples (calls to PRNG) times & concatenate all the bits obtained
    • Check if the resulting number is within your range
    • Yes?: return number (finished)
    • No?: do nothing (loop continues; will resample all components again!)

Remarks

  • This is a form of acceptance-sampling / rejection-sampling
  • The approach is a Las-vegas type of algorithm: the runtime is not bounded in theory
    • The number of loops needed is in average: n_possible-sample-numbers-of-full-concatenation / n_possible-sample-numbers-within-range
  • The complete resampling (if result not within range) according to the rejection-method is giving access to more formal-analysis of non-bias / uniformity and is a very important aspect for this approach
  • Of course the classic assumptions in regards to PRNG-output are needed to make this work.
    • If the PRNG for example has some non-uniformity in regards to low-bits / high-bits (as often mentioned), this will have an effect of the output above
like image 90
sascha Avatar answered Oct 03 '22 17:10

sascha


I was looking at this problem from the wrong angle

The bins are not the same size. Each bin is 10 times the size of the previous one. To put this in perspective, there are 10,000 possible integers at magnitude 1e+44 for every integer with magnitude 1e+40.

The probability of finding any number with magnitude 1e+20 for the bigint at 1e+45 is less than 0.00000 00000 00000 00000 001 %.

Forget needles in haystacks, this is more like finding a needle in a quasar!

like image 23
Zaid Avatar answered Oct 03 '22 17:10

Zaid