Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question concerns the generation of, and use of, pseudo random numbers. (i

ID: 3780550 • Letter: T

Question



This question concerns the generation of, and use of, pseudo random numbers. (i write a C++ function that uses the linear congruential generator method to produce unsigned pseudo-random 16 bit integers. Use predefined constant values for a 16007 and b 2147483647 (i) write a well commented Ctt program to repeatedly generate a set of 5 uniformly distributed integer random numbers in the range to 50] and 2 random integer numbers in the range [1 to 91. Use your function from part til and generate a seed value for x using the computer's clock, (Hint: find a suitable C++ library function

Explanation / Answer

Part i)

#include <iostream>

using namespace std;

class RandomGen
{
public:
void seed(unsigned int s)
{
_seed = s;
}

protected:
RandomGen() :
_seed(0), _a(0), _b(0), _m(2147483648)
{
}
int rnd()
{
return (_seed = (_a * _seed + _b) % _m);
}

int _a, _b;
unsigned int _m, _seed;
};

class MsRandomGen: public RandomGen
{
public:
MsRandomGen()
{
_a = 16807;
_b = 2147483647;
}
int rnd()
{
return RandomGen::rnd() >> 16;
}
};


int main(int argc, char* argv[])
{
  
MsRandomGen ms_rnd;

cout << " RAND:" << endl << "========" << endl;
for (int x = 0; x < 10; x++)
cout << ms_rnd.rnd() << endl;


return 0;
}

========================================================

akshay@akshay-Inspiron-3537:~/Chegg$ g++ linearrando.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
RAND:
========
32767
32767
28457
3697
22003
30691
2027
25429
27597
3728

=========================================================

Part ii

#include <iostream>

using namespace std;

class RandomGen
{
public:
void seed()
{
_seed = clock();
}

protected:
RandomGen() :
_seed(), _a(0), _b(0), _m(2147483648)
{
}
int rnd()
{
return (_seed = (_a * _seed + _b) % _m);
}

int _a, _b;
unsigned int _m, _seed;
};

class MsRandomGen: public RandomGen
{
public:
MsRandomGen()
{
_a = 16807;
_b = 2147483647;
}
int rnd()
{
return RandomGen::rnd() >> 16;
}
};


int main(int argc, char* argv[])
{
  
MsRandomGen ms_rnd;

cout << " RANDOM Numbers in range 1 to 50:" << endl << "========" << endl;
for (int x = 0; x < 5; x++)
cout << ms_rnd.rnd()%50+1 << endl;

cout << " RANDOM Numbers in range 1 to 9:" << endl << "========" << endl;
for (int x = 0; x < 2; x++)
cout << ms_rnd.rnd()%9+1 << endl;


return 0;
}

==============================================================

akshay@akshay-Inspiron-3537:~/Chegg$ g++ linearrando.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
RANDOM Numbers in range 1 to 50:
========
18
18
8
48
4
RANDOM Numbers in range 1 to 9:
========
2
3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote