0

I'm looking for a function to generate random numbers in [a,b]

Function must take one argument - seed and return random x є [a,b]. The probability of x = a is the lowest, but is increasing when moving to b. The probability of x = b should be the highest.

As far as I understand what I'm looking for is an implementation of cumulative distribution function, but can't say for sure. So i'll be glad for direct literature. Or maybe there's an in-box php implementation already?

Thanks

3
  • What have you tried? If you do not show some code, folks will close this faster than you can blink. Commented Dec 11, 2012 at 18:59
  • Possible duplicate of stackoverflow.com/questions/4304765/… Commented Dec 11, 2012 at 19:00
  • I haven't tried coding yet... Can't imagine where to start Commented Dec 11, 2012 at 19:04

1 Answer 1

1

First of all, you did not specify what an argument should be. The function below does not use comulative distribution. Incereasing the $n will cause values close to b more likely to be returned.

function almostRandom() {
    $a = 1;
    $b = 10;
    $n = 2.5;

    $random = rand(0, 1000000) / 1000000;

    $positionInInterval = 1 - pow($random, $n);

    $intervalLength = $b - $a;
    $value = $a + $intervalLength * $positionInInterval;

    return $value;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This actually works, but I can't understand it at all. The argument is a "seed" (just recently found out how it's called)... Thanks!
Use Wolfram Alpha to plot a graph of 1 - x^2.5 You will be able to see, that if x is random number between 0 and 1, the value of the graph is more likely to be closer to 1 than to 0. You can set the seed for random generator with srand function php.net/manual/en/function.srand.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.