- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Random-0-6-Generator using the given Random-0-1-Generator
In this article, our primary aim is to come up with a solution to implement a random-0-6-Generator using the given random-0-1-Generator. As we know,
A random-0-1-Generator() function returns either 0 or 1 as the output. Similarly, a random-0-6-Generator, as the name suggests, gives any random numbers between 0 and 6 (including 0 and 6).
Also, a main point to keep in mind is that the random-0-6-Generator should generate random numbers ranging between 1 to 6, with equal probability. That is the probability of obtaining any number should always be the same.
Example
For instance, the random values or the outputs a random-0-6-Generator delivers on repeated runs are given here;
3 4 6 1 2 6 1 1 0
Note that the number repeats and the probability of occurrence of any number within the range that is, 0 to 6 (both inclusive) is the same.
Problem Statement
Implement a function that uses the random01Generator() function to generate numbers between 0 and 6 (both inclusive). All numbers should have the same chance of occurring.
Approach
The goal in this case is to generate random numbers within the specified range.Given that the range from 0 to 6, including the numbers 0 and 6. Here we use the rand function. In order to create simulations, games, and other activities that call for random events, we frequently need to use the rand function in our code.
For instance, if there are no random events in a dice game, we will always get the same side when we roll the dice, which will provide unfavorable outcomes. Thereby, it becomes essential that we have access to a random number generator. That is a rand function which generates random numbers.
What is a rand function?
The rand function of the cstdlib standard library reverts a pseudo-random number between 0 and RAND MAX. Coming to RAND MAX, it is an environment-dependent value that represents the highest value brought back by the rand function.
In our case since the range is 0 to 6, the RAND MAX is 6. So here the rand function returns a random integer between 0 and 6 (including 0 and 6) each time we run the program.
Algorithm
The algorithm to Implement a function that uses the rand function to generate numbers between 0 and 6 (both inclusive) is given below.
Step 1: Start
Step 2: Set n=1 and define variable random.
Step 3: Choose a range. Here it is set to 0 and 6.
Step 4: Use the rand() function.
Step 5: Return the random value.
Step 6: Stop
Given below is a simple c++ program to generate random integers between 0 and 6 using the rand function.
Example
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; int main() { int n=1, random; //set n equals 1 // print a random number using rand function cout << "Random number between 0 and 6\n"; while(n--){ random = rand()%6 + 1; //set the RAND MAX, that is 6 cout << random << " "; //return a random integer } return 0; }
Output
On execution, it will produce following output:
Random number between 0 and 6 2
Conclusion
This program returns a random integer between 0 and 6 each time we run it. And the probability of occurrence of any number within this range is equal. Similarly, you can give any range of your choice and the program will return a random integer within the given range.
The challenge of obtaining random integers between 0 and 6 is resolved in this article. C++ programming code to return a random integer between 0 and 6 is provided in this article.