In: Computer Science
in c++, without using any built-in random library already exist, instead,write your own function to generate random char from a list of:
10 letters T
10 letters H
10 letters A
10 letters N
10 letters K
Generally, we use rand() function to generate random number in given range.
Here, we are not allowed to use any built-in random library.
Let's think differently to solve task alloted to us.
What is else which is randomly alloted in program?...
There are many things which are randomly allocated to program while running like Processor, Turn, position in queue, memory etc..
We could easily access memory address using pointer . Yes we could use pointer to generate random within given range.. Let's check it
Here is a code snippet which will print random character from list of characters..
#include<bits/stdc++.h>
using namespace std;
int main()
{
string t(10,'T');
string h(10,'H');
string a(10,'A');
string n(10,'N');
string k(10,'K');
string mylist=t+h+a+n+k;
int*p=new int; ///Random memory allocated to p. We will use memory address to generate random integer
int myrand;
myrand=(int)p%mylist.size(); ///limiting range of integer to size of list
/// cout<<myrand; This line will print random integer between 0-50
cout<<"Random Character Generated is : "<<mylist[myrand];
}
OUTPUT'S SCREENSHOT
1.
2.
3