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 9000017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:15:25+00:00 2026-06-16T00:15:25+00:00

I want to generate a random number with a given probability but I’m not

  • 0

I want to generate a random number with a given probability but I’m not sure how to:

I need a number between 1 and 3

num = ceil(rand*3);

but I need different values to have different probabilities of generating eg.

0.5 chance of 1
0.1 chance of 2
0.4 chance of 3

I’m sure this is straightforward but I can’t think of how to do it.

  • 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-06-16T00:15:26+00:00Added an answer on June 16, 2026 at 12:15 am

    The simple solution is to generate a number with a uniform distribution (using rand), and manipulate it a bit:

    r = rand;
    prob = [0.5, 0.1, 0.4];
    x = sum(r >= cumsum([0, prob]));
    

    or in a one-liner:

    x = sum(rand >= cumsum([0, 0.5, 0.1, 0.4]));
    

    Explanation

    Here r is a uniformly distributed random number between 0 and 1. To generate an integer number between 1 and 3, the trick is to divide the [0, 1] range into 3 segments, where the length of each segment is proportional to its corresponding probability. In your case, you would have:

    • Segment [0, 0.5), corresponding to number 1.
    • Segment [0.5, 0.6), corresponding to number 2.
    • Segment [0.6, 1], corresponding to number 3.

    The probability of r falling within any of the segments is proportional to the probabilities you want for each number. sum(r >= cumsum([0, prob])) is just a fancy way of mapping an integer number to one of the segments.

    Extension

    If you’re interested in creating a vector/matrix of random numbers, you can use a loop or arrayfun:

    r = rand(3); % # Any size you want
    x = arrayfun(@(z)sum(z >= cumsum([0, prob])), r);
    

    Of course, there’s also a vectorized solution, I’m just too lazy to write it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate random number between 0 to 3.8 million in python but
What I want to do is to generate a random number between and creating
I want to generate random integers between 0-9 (inclusive on both ends), but I
I want to generate random number in a specific range. (Ex. Range Between 65
I want to generate a random number with 1 decimal point between min_time and
So let's say you want to generate a random number, but you want it
I want to generate random number, which is 9 digits including leading zero if
i am trying to generate random number for my mental math quiz game. But
Is there a built in function that can generate a random prime number between
I want to generate 2 random numbers between 0 and 20 int one =

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.