Open In App

Implement rand12() using rand6() in one line

Last Updated : 08 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a function, rand6() that returns random numbers from 1 to 6 with equal probability, implement the one-liner function rand12() using rand6() which returns random numbers from 1 to 12 with equal probability. The solution should minimize the number of calls to the rand6() method. Use of any other library function and floating point arithmetic are not allowed.

The idea is to use expression rand6() + (rand6() % 2) * 6. It returns random numbers from 1 to 12 with equal probability. The expression is equivalent to –

// if rand6() is even
if (rand6() % 2)
    return 6 + rand6();
else // if rand6() is odd
    return rand6();

We can also use any one of the below expressions that works in a similar way –

  • rand6() + !(rand6() % 2) * 6 or
  • rand6() + (rand6() & 1) * 6 or
  • rand6() + !(rand6() & 1) * 6

Below is the implementation of the above approach:

C++
#include <cstdlib> // for rand and srand
#include <ctime> // for time
#include <iostream>

using namespace std;

// Function that returns random numbers from 1 to 6 with
// equal probability
int rand6()
{
    // rand() will generate random numbers between 0 and
    // RAND_MAX with equal probability rand() % 6 returns
    // number from 0 to 5 with equal probability (rand() %
    // 6) + 1 returns number from 1 to 6 with equal
    // probability
    return (rand() % 6) + 1;
}

// The function uses rand6() to return random numbers from 1
// to 12 with equal probability
int rand12() { return rand6() + (rand6() % 2) * 6; }

// Driver code to test above functions
int main()
{
    // Initialize random number generator
    srand(time(NULL));
    int N = 12;

    int freq[N + 1] = { 0 };

    // call rand12() multiple times and store its results
    for (int i = 0; i < N * 100000; i++)
        freq[rand12()]++;

    // print frequency of numbers 1-12
    for (int i = 1; i <= N; i++)
        cout << freq[i] << " ";

    return 0;
}
Java Python C# JavaScript

Output:

100237 100202 99678 99867 100253 99929 100287 100449 99827 99298 100019 99954 

Another Solution:

int rand12()
{
    return (rand6() * 2) - (rand6() & 1);
}
  • rand6() * 2: This will return even numbers 2, 4, 6, 8, 10 and 12 with equal probability and
  • rand6() & 1: This will return 0 or 1 based on rand6() is even or odd respectively. So, the expression
  • (rand6() * 2) – (rand6() & 1): This will return random numbers from 1 to 12 with equal probability.

Below is the implementation of the above approach:

C++
#include <cstdlib>
#include <ctime>
#include <iostream>

// Assume rand6 generates a random integer between 1 and 6
int rand6() {
    return rand() % 6 + 1;
}

int rand12() {
    return (rand6() * 2) - (rand6() & 1);
}

int main() {
    // Seed the random number generator
    srand(time(0));

    // Generate a random number between 1 and 12
    int randomNum = rand12();

    // Print the random number
    std::cout << "Random number between 1 and 12: " << randomNum << std::endl;

    return 0;
}
Java Python JavaScript

Output
Random number between 1 and 12: 7


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg