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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:25:14+00:00 2026-05-27T12:25:14+00:00

I would like to generate a random number between 0 and 3 and I

  • 0

I would like to generate a random number between 0 and 3 and I have the following in my code:

int random = rand() % 4;

This works fine but I would like it to generate 1, 2, and 3 most of the time and 0 only occasionally.

What is the best way to go about this? What are the names of common algorithms to address this problem?

  • 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-27T12:25:14+00:00Added an answer on May 27, 2026 at 12:25 pm

    Here’s one way. Suppose you want 0, 1, 2, 3 to have a distribution of 5%, 20%, 30%, 45%.
    You could do it like this:

    double val = (double)rand() / RAND_MAX;
    
    int random;
    if (val < 0.05)       //  5%
        random = 0;
    else if (val < 0.25)  //  5% + 20%
        random = 1;
    else if (val < 0.55)  //  5% + 20% + 30%
        random = 2;
    else
        random = 3;
    

    Of course it doesn’t have to be done with floating-point. I just did it this way since it’s more intuitive.

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

Sidebar

Related Questions

would like to print a random number between 0 and 10, but generate seems
I would like to generate a random floating point number between 2 values. What
I would like to generate a random number string of N length with this
I would like to know how to generate a random number between two given
Given a regexp, I would like to generate random data x number of time
I have an unsigned int storing a random number. I need to use this
I am using arc4random to generate a random number. I would like to generate
I would like to generate random numbers between 1 & 10, with a decimal
I would like to generate uniform random numbers in C++ between 0 and 1,
So, I've got my code ready to generate and print a random number between

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.