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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:10:24+00:00 2026-05-12T08:10:24+00:00

How can I generate a bigger probability set from a smaller probability set? This

  • 0

How can I generate a bigger probability set from a smaller probability set?

This is from Algorithm Design Manual -Steven Skiena

Q:

Use a random number generator (rng04) that generates numbers from {0,1,2,3,4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability?

I tried for around 3 hours now, mostly based on summing two rng04 outputs. The problem is that in that case the probability of each value is different – 4 can come with 5/24 probability while 0 happening is 1/24. I tried some ways to mask it, but cannot.

Can somebody solve this?

  • 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-12T08:10:24+00:00Added an answer on May 12, 2026 at 8:10 am

    You have to find a way to combine the two sets of random numbers (the first and second random {0,1,2,3,4} ) and make n*n distinct possibilities. Basically the problem is that with addition you get something like this

            X
          0 1 2 3 4
    
      0   0 1 2 3 4
    Y 1   1 2 3 4 5
      2   2 3 4 5 6
      3   3 4 5 6 7
      4   4 5 6 7 8
    

    Which has duplicates, which is not what you want. One possible way to combine the two sets would be the Z = X + Y*5 where X and Y are the two random numbers. That would give you a set of results like this

            X
           0  1  2  3  4
    
      0    0  1  2  3  4
    Y 1    5  6  7  8  9
      2   10 11 12 13 14
      3   15 16 17 18 19
      4   20 21 22 23 24
    

    So now that you have a bigger set of random numbers, you need to do the reverse and make it smaller. This set has 25 distinct values (because you started with 5, and used two random numbers, so 5*5=25). The set you want has 8 distinct values. A naïve way to do this would be

    x = rnd(5)  // {0,1,2,3,4}
    y = rnd(5)  // {0,1,2,3,4}
    z = x+y*5   // {0-24}
    random07 = x mod 8
    

    This would indeed have a range of {0,7}. But the values {1,7} would appear 3/25 times, and the value 0 would appear 4/25 times. This is because 0 mod 8 = 0, 8 mod 8 = 0, 16 mod 8 = 0 and 24 mod 8 = 0.

    To fix this, you can modify the code above to this.

    do {
      x = rnd(5)  // {0,1,2,3,4}
      y = rnd(5)  // {0,1,2,3,4}
      z = x+y*5   // {0-24}
    while (z != 24)
    
    random07 = z mod 8
    

    This will take the one value (24) that is throwing off your probabilities and discard it. Generating a new random number if you get a ‘bad’ value like this will make your algorithm run very slightly longer (in this case 1/25 of the time it will take 2x as long to run, 1/625 it will take 3x as long, etc). But it will give you the right probabilities.

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

Sidebar

Related Questions

In Clearcase I can generate a label for a given set of files and
Is there a tool that can generate sequence diagrams from running .NET code?
Is there an IDE out there that can generate XML from XSD? Not Oxygen
In F#, you can generate a set of numbers, just by saying [1..100]. I
How can I generate a (pseudo)random alpha-numeric string, something like: 'd79jd8c' in PHP?
How can i generate bytecode (Byte[]) from a String at runtime, without using a
I'm looking for a tool which can generate a Makefile for a C/C++ project
I have a HQL query that can generate either an IList of results, or
While for the most part you can generate the same MSIL, there are definitely
I'm working on a small app where I can generate a list of barcodes.

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.