Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate a random number between 1 and x where a lower number is more likely than a higher one

Tags:

php

random

math

This is more of a maths/general programming question, but I am programming with PHP is that makes a difference.

I think the easiest way to explain is with an example.

If the range is between 1 and 10.

I want to generate a number that is between 1 an 10 but is more likely lower than high.

The only way I can think is generate an array with 10 elements equal to 1, 9 elements equal to 2, 8 elements equal to 3.....1 element equal to 10. Then generate a random number based on the number of elements.

The trouble is I am potentially dealing with 1 - 100000 and that array would be ridiculously big.

So how best to do it?

like image 983
Pablo Avatar asked Jul 21 '10 15:07

Pablo


People also ask

How do you generate a random number greater than or equal to zero and less than one in Java?

Use Math. Math. random() returns a double type pseudo-random number, greater than or equal to zero and less than one.

What is the most common random number between 1 and 10?

According to the video, if you ask people to randomly pick any one integer between 1 and 10 (both inclusive), people are more likely to choose 7.

How do you generate a random number from a probability distribution?

In general, generating a random number from a probability distribution means transforming random numbers so that the numbers fit the distribution. Perhaps the most generic way to do so is called inverse transform sampling: Generate a uniform random number in [0, 1].


2 Answers

Generate a random number between 0 and a random number!

like image 74
Tim Rogers Avatar answered Oct 13 '22 22:10

Tim Rogers


Generate a number between 1 and foo(n), where foo runs an algorithm over n (e.g. a logarithmic function). Then reverse foo() on the result.

like image 31
Quentin Avatar answered Oct 13 '22 23:10

Quentin