# Implement rand12() using rand6() in one line

Given a function rand6() that returns random numbers from 1 to 6 with equal probability, implement one-liner function rand12() using rand6() that returns random numbers from 1 to 12 with equal probability. Solution should minimize the number of calls to 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 evenif (rand6() % 2)    return 6 + rand6();else // if rand6() is odd    return rand6();`

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

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

Below is C++ implementation of above idea –

## CPP

 `// C++ Program to print random numbers from 1 to 12` `// with equal probability using a function that returns` `// random numbers from 1 to 6 with equal probability` `#include ` `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 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

 `import` `java.util.Random;`   `public` `class` `RandomNumbers {` `    ``public` `static` `int` `rand6() {` `        ``Random random = ``new` `Random();` `        ``//nextInt(6) generates a random number from 0 to 5` `        ``return` `random.nextInt(``6``) + ``1``;` `    ``}`   `    ``public` `static` `int` `rand12() {` `        ``return` `rand6() + (rand6() % ``2``) * ``6``;` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``int` `N = ``12``;` `        ``int``[] freq = ``new` `int``[N + ``1``];`   `        ``// Initialize random number generator` `        ``Random random = ``new` `Random();`   `        ``// Call rand12() multiple times and store its results` `        ``for` `(``int` `i = ``0``; i < N * ``100000``; i++) {` `            ``freq[rand12()]++;` `        ``}`   `        ``// Print the frequency of numbers 1-12` `        ``for` `(``int` `i = ``1``; i <= N; i++) {` `            ``System.out.print(freq[i] + ``" "``);` `        ``}` `    ``}` `}`

## C#

 `using` `System;`   `class` `Program` `{` `    ``// Create a single instance of Random for better performance` `    ``static` `Random rand = ``new` `Random();`   `    ``// Function that returns random numbers from 1 to 6 with equal probability` `    ``static` `int` `Rand6()` `    ``{` `        ``return` `rand.Next(1, 7);` `    ``}`   `    ``// The function uses Rand6() to return random numbers from 1 to 12 with equal probability` `    ``static` `int` `Rand12()` `    ``{` `        ``return` `Rand6() + (Rand6() % 2) * 6;` `    ``}`   `    ``// Driver code to test above functions` `    ``static` `void` `Main()` `    ``{` `        ``int` `N = 12;` `        ``int` `iterations = 100000;`   `        ``int``[] freq = ``new` `int``[N + 1];`   `        ``// call Rand12() multiple times and store its results` `        ``for` `(``int` `i = 0; i < iterations; i++)` `            ``freq[Rand12()]++;`   `        ``// print frequency of numbers 1-12` `        ``for` `(``int` `i = 1; i <= N; i++)` `            ``Console.Write(freq[i] + ``" "``);`   `        ``Console.ReadLine(); ``// Added to keep the console window open until a key is pressed` `    ``}` `}`

## Javascript

 `function` `rand6() {` `  ``// Generates a random number from 1 to 6` `  ``return` `Math.floor(Math.random() * 6) + 1;` `}`   `function` `rand12() {` `  ``return` `rand6() + (rand6() % 2) * 6;` `}`   `const N = 12;` `const freq = ``new` `Array(N + 1).fill(0);`   `// Call rand12() multiple times and store its results` `for` `(let i = 0; i < N * 100000; i++) {` `  ``freq[rand12()]++;` `}`   `// Print the frequency of numbers 1-12` `for` `(let i = 1; i <= N; i++) {` `  ``console.log(freq[i]);` `}`

Output:

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

Another Solution –

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

rand6() * 2

will return even numbers 2, 4, 6, 8, 10 and 12 with equal probability and

rand6() & 1

will return 0 or 1 based on rand6() is even or odd respectively. So, the expression

(rand6() * 2) – (rand6() & 1)

will return random numbers from 1 to 12 with equal probability.

Please note above solutions will produce different results every time we run them.

If you like GeeksforGeeks and would like to contribute, you can also write an article using

write.geeksforgeeks.org

or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!