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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:07:40+00:00 2026-05-16T03:07:40+00:00

To get a random whole number between 0 and 4 inclusive, I use the

  • 0

To get a random whole number between 0 and 4 inclusive, I use the following code:

var random = Math.floor(Math.random() * 5);

However I want the number 4 to be chosen twice as often as the other numbers. How would I go about doing getting a random number between 0-4 inclusive, but with the number 4 appearing twice as many times as 0-3?

  • 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-16T03:07:41+00:00Added an answer on May 16, 2026 at 3:07 am

    I think the easist way is to add the number twice to the array:

    var nums = [0, 1, 2, 3, 4, 4];
    var index = Math.floor(Math.random() * 6);
    var random = nums[index];
    

    But for something more generic, you may have a table:

    var nums = [0, 1, 2, 3, 4];
    var odds = [0.16667, 0.16667, 0.16667, 0.16667, 0.33333];
    var r = Math.random();
    var i = 0;
    while(r > 0)
      r -= odds[i++];
    var random = nums[i];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get a random number in pascal from between a range. Basically
Basically I pick a random number between 0-24: Math.floor(Math.random() * myArray.length); // myArray contains
Here's the code: <?php $r=rand(1,7) //get a random number from 1 to 7 inclusive...
I want to get n random numbers(e.g n=16)(whole numbers) between 1 to 5(including both)
This following code will get me a random number: Random r = new Random();
I'm running the following code to get a random entry from a dictionary: SELECT
I want to get random numbers with exceptions (for example, I want a number
I want to get N random numbers whose sum is a value. For example,
I want to get random keys and their respective values from a Map. The
I need to get some random colors to draw a pie. My code works

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.