How can I shift the probability of a random number generator as it moves through a list?

Viewed 152

I'm working on a side project which involves randomly generating neural networks using PyTorch. Issue is it's inefficient to randomly generate the sizes of the hidden layers if the input and output layers vary too much. So I have to kind of "scale" the sizes of the hidden layers as they reach the output layer while keeping the randomness part of it.

Let's say I have two numbers (1 and 20) and I wanted to make a list using a random number generator which picks through a range of numbers in between 1 and 20. But I also wanted to shift the probability of numbers being picked as it adds to that list.

So if it has already added 15 numbers, then the 16th number's probability for being the number 16 is much greater than its probability is for being number 1 so-to-speak - but if the code is adding the first number to the list, then its probability for being 1 is far greater.

myList = generateRandList(1, 20)

print(myList)

[2, 2, 3, 4, 6, 3, 7, 6, 9, 10, 12, 11, 13, 12, 15, 16, 18, 16, 19, 20]

I was working with numpy.random.choice() and that does vary the probability of numbers being picked from a list, but not as it moves through the list.

1 Answers

You want to use a triangular distribution where the mode is the highest bound, that will be a linear distribution where N has the highest probability and 0 has lowest:

import random
[int(random.triangular(1,n,n)) for n in range(1, 20)]

Using numpy as suggested by Mr. T:

import numpy as np
gen = np.random.default_rng()
np.fromiter((int(gen.triangular(1, n, n)) for n in range(2,20)), dtype=np.uint64)

Numpy is a bit more intensive in the checks it performs, I had to do some modifications on the lower bound.

Related