Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 920015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:38:18+00:00 2026-05-15T18:38:18+00:00

I want a random number generator with non-uniform distribution, ie: // prints 0 with

  • 0

I want a random number generator with non-uniform distribution, ie:

// prints 0 with 0.1 probability, and 1 with 0.9 probability
echo probRandom(array(10, 90));

This is what I have right now:

/**
 * method to generated a *not uniformly* random index
 *
 * @param array $probs int array with weights 
 * @return int a random index in $probs
 */
function probRandom($probs) {
    $size = count($probs);

    // construct probability vector
    $prob_vector = array();
    $ptr = 0;
    for ($i=0; $i<$size; $i++) {
        $ptr += $probs[$i]; 
        $prob_vector[$i] = $ptr;
    }

    // get a random number
    $rand = rand(0, $ptr);
    for ($i=0, $ret = false; $ret === false; $i++) {
        if ($rand <= $prob_vector[$i])
            return $i;
    }   
}

Can anyone think of a better way? Possibly one that doesn’t require me to do pre-processing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:38:19+00:00Added an answer on May 15, 2026 at 6:38 pm

    In your solution you generate an accumulated probability vector, which is very useful.

    I have two suggestions for improvement:

    • if $probs are static, i.e. it’s the same vector every time you want to generate a random number, you can preprocess $prob_vector just once and keep it.
    • you can use binary search for the $i (Newton bisection method)

    EDIT: I now see that you ask for a solution without preprocessing.

    Without preprocessing, you will end up with worst case linear runtime (i.e., double the length of the vector, and your running time will double as well).

    Here is a method that doesn’t require preprocessing. It does, however, require you to know a maximum limit of the elements in $probs:

    Rejection method

    • Pick a random index, $i and a random number, X (uniformly) between 0 and max($probs)-1, inclusive.
    • If X is less than $probs[$i], you’re done – $i is your random number
    • Otherwise reject $i (hence the name of the method) and restart.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte array generated by a random number generator. I want to
I have a pseudo random number generator (PRNG) class that I want to unit
All I want is a pragmatic random number generator in C# so I can
I want to place obstacles on a game board using a random number generator.
I have a random number generator (RNG) function. -(double) generateRandomNumber { ... do some
I have a program that uses the mt19937 random number generator from boost::random. I
If I want to use the gaussian random number generator in MATLAB R =
Duplicate: Unique random numbers in O(1)? I want an pseudo random number generator that
I want to use a random number generator that creates random numbers in a
So lets assume I have a Gaussian random number generator that takes 2 parameters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.