Generate a uniform random integer from 1 to 12 using only a fair six-sided die function
Quick Overview
Build a function that returns a uniform random integer from 1 to 12 when the only randomness available is a function returning a uniform integer from 1 to 6. It tests whether you can prove a mapping of dice outcomes is exactly uniform and tell when rejection sampling is needed.
Generate a uniform random integer from 1 to 12 using only a fair six-sided die function
Company: Swiggy
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
You are given a function `f6()` that returns a uniformly random integer between 1 and 6, each value with probability 1/6. Implement a function `f12()` that returns a uniformly random integer between 1 and 12, each value with probability 1/12.
You may only use `f6()` as your source of randomness. The distribution over 1 to 12 must be exactly uniform, and the function must always return a valid value (it may retry internally if needed).
```hint Count the outcomes
Two independent calls to `f6()` produce a fixed number of equally likely ordered pairs. Compare that count with 12.
```
```hint Check the combining step
Some natural ways to combine two rolls, such as adding them, do not give equally likely results. Work out the probability of each output before you trust a formula.
```
### Constraints & Assumptions
- Each call to `f6()` is independent and uniform over 1 to 6.
- `f6()` is the only source of randomness; no other random number generator is available.
- Every value from 1 to 12 must have probability exactly 1/12.
- The expected number of `f6()` calls per `f12()` call must be finite.
### Clarifying Questions to Ask
- Are successive calls to `f6()` independent of each other?
- Is a finite expected number of `f6()` calls enough, or must the worst case be bounded?
- Should the solution minimize the number of `f6()` calls?
### What a Strong Answer Covers
- How two or more calls to `f6()` are combined into one value that is uniform over a known range.
- How that range is mapped onto 1 to 12 so every output receives the same number of outcomes, with a short argument that it does.
- Whether rejection is needed for this pair of numbers, and the expected number of `f6()` calls of the chosen method.
- Why tempting shortcuts such as adding two rolls are not uniform.
- How the function could be tested for uniformity.
### Follow-up Questions
- Implement `f7()` using only `f5()`. What is the expected number of `f5()` calls?
- Generalize the method to return a uniform integer from 1 to N given a function that returns a uniform integer from 1 to M.
- When rejection is needed, how can the rejected outcomes be reused to lower the expected number of calls?
- With a chi-squared goodness-of-fit test on samples of `f12()`, roughly how many samples do you need to detect one output whose probability is off by one percentage point?
Overview: Build a function that returns a uniform random integer from 1 to 12 when the only randomness available is a function returning a uniform integer from 1 to 6. It tests whether you can prove a mapping of dice outcomes is exactly uniform and tell when rejection sampling is needed.
Generate a uniform random integer from 1 to 12 using only a fair six-sided die function
Swiggy
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
You are given a function f6() that returns a uniformly random integer between 1 and 6, each value with probability 1/6. Implement a function f12() that returns a uniformly random integer between 1 and 12, each value with probability 1/12.
You may only use f6() as your source of randomness. The distribution over 1 to 12 must be exactly uniform, and the function must always return a valid value (it may retry internally if needed).
Constraints & Assumptions
Each call to
f6()
is independent and uniform over 1 to 6.
f6()
is the only source of randomness; no other random number generator is available.
Every value from 1 to 12 must have probability exactly 1/12.
The expected number of
f6()
calls per
f12()
call must be finite.
Clarifying Questions to Ask Guidance
Are successive calls to
f6()
independent of each other?
Is a finite expected number of
f6()
calls enough, or must the worst case be bounded?
Should the solution minimize the number of
f6()
calls?
What a Strong Answer Covers Guidance
How two or more calls to
f6()
are combined into one value that is uniform over a known range.
How that range is mapped onto 1 to 12 so every output receives the same number of outcomes, with a short argument that it does.
Whether rejection is needed for this pair of numbers, and the expected number of
f6()
calls of the chosen method.
Why tempting shortcuts such as adding two rolls are not uniform.
How the function could be tested for uniformity.
Follow-up Questions Guidance
Implement
f7()
using only
f5()
. What is the expected number of
f5()
calls?
Generalize the method to return a uniform integer from 1 to N given a function that returns a uniform integer from 1 to M.
When rejection is needed, how can the rejected outcomes be reused to lower the expected number of calls?
With a chi-squared goodness-of-fit test on samples of
f12()
, roughly how many samples do you need to detect one output whose probability is off by one percentage point?