Hi Guys.

Random no.'s always bother me.So,I want to start a discussion over their uniform distribution.

Given : rand5() which generates randomly distributed no. from 1-5.
To Code: rand7() which generates randomly distributed no. from 1-5.

You have to give code using rand5() as well as without using it.


Thanks,

Dani AI

Generated

A few clarifications and a practical solution that fit the thread.

— summing several rand5() calls and dividing (then rounding) will NOT produce a uniform 1..7 outcome. The sum of discrete uniform variables follows a convolution (triangular-ish) distribution, so some totals are far more likely; simple division/rounding creates bias.

The standard, unbiased approach is rejection sampling using base-5 digits. Combine two independent rand5() calls to build a uniform integer in 0..24. Accept the values 0..20 (21 outcomes); mapping value % 7 + 1 yields a perfect uniform 1..7 because 21 is a multiple of 7. If the value is 21..24, reject and repeat. Expected rand5() calls ≈ 2.38 (2 calls per attempt, accept rate 21/25).

Example (concise Python):

def rand7():
    while True:
        v = (rand5() - 1) * 5 + (rand5() - 1)   # v in 0..24
        if v < 21:
            return (v % 7) + 1

pointed toward the same reduction idea — this is that approach written out more explicitly and language-agnostic. For slightly different tradeoffs: using three rand5() calls gives a 0..124 range, accept 0..118 (119/125 ≈ 95.2% accept) but costs more per attempt (expected ≈ 3.15 calls). A different alternative is to extract fair bits from rand5() (reject on the extra outcome) and build a 3-bit number; that works but is less efficient on average.

A useful optimization is to recycle rejected remainders instead of restarting from scratch: when you get 21..24, treat that leftover as extra entropy and append another rand5() to expand the range (classic multi-stage rejection). It reduces the expected number of calls but adds implementation complexity.

Note: these assume rand5() returns unbiased integers 1..5. If rand5() returns floats, discretize carefully or convert to an integer uniform source first. For correctness, always test with a chi-squared check to confirm uniformity.

Recommended Answers

All 2 Replies

If we call rand5() seven times and add up the results, we would get an integer in the inclusive interval [7,35].
Divide this number by 5.0 and we would get a real number in the inclusive interval [1.0,7.0].

Take care of the round-off correctly (how?) and you have rand7().

First off given your question statement,

To Code: rand7() which generates randomly distributed no. from 1-5

you can do this: ;)

int rand7() {  return rand5(); }

Ok I guess that isn't your question, you actually want to return rand7 where the result is 1-7.

First thing to establish is if rand5() returns an integer value or a floating point number in the rand 1-5. If it is integer you can try this:

Quicker in the long run is to use a reduction algorithm. E.g create the number

//
while(a>20) {
  a=(rand5()-1)*5+(rand5()-1);
}
return 1+a/7;

The issue is is it quicker than adding 7 numbers (and seven calls to rand5()). It can be slower, but most of the time it is quicker.

If on the other hand you have 1.0->5.0 you can actually use the same algorithms, but in that case there is the much quicker approach of just dividing the result, however, this approach looses low bit accuracy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.