Philip Newton
Philip Newton

Reputation: 9

Main.cpp and a Function for Random number Generator C++

Hi I'm a newbie up here and my hope to get it right rest here. Last semester we dealt with Random number generator me and my friend we were able to come up with a simple program (below) which worked:

using namespace std;

int randomG() {
    int GER(0);
    GER=rand();
    GER %= 10;
    GER++;
    return(GER); 
}

int main() {
    srand((unsigned)time(NULL));
    for(int i = 0; i != 100; i++) {
        cout << randomG(); 
    }
}

Now this semester we got given this without going much into it. Basically he want us to To implement the random number generator with main.cpp that call a function FNC multiple times, to test the program we need to use state=1 as initial value. Call FNC from the main program. After 10000 calls: state should be equal to 399268537 (don't get what he mean here)

Below is our starting point:

double rand (void) {
    const long a=48271            //multiplier
    const long b=2147483647      //modulus
    const long c=b/a             //questient
    const long r=b % a           //remainder
    static long state =1;
    long t = a* (state % c) - r * (state/c);
    if(t >0)
        state=t;
    else
        state = t+b;
    return ((double) state / b);
}

for main.cpp we completely lost on what to do, we have no idea on how we going to call the function and coming up with a table from the program output for first 1 to 10 calls and for 9991 to 10000 calls of FNG. we can't move any further. For sophomore like us, this is total confusion. any help will be apprecaited

int main() {
    srand((unsigned)time(NULL));
    int                        // we lost on what we should put in our main

After help from you guys Below is my codes but it still not compiling what am I missing here?

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

double seed(long state) {
    g_state = state;
}

double rand(void) {
    const long a = 48271;
    const long b = 2147483647;
    const long c = b / a;
    const long r = b % a;
    long t = a* (g_state % c) - r * (g_state / c);
    if (t > 0)
        g_state = t;
    else
        g_state = t + b;
    return ((double)g_state / b);
}


int main() {
    std::vector<double> results;
    for (int i = 1; i <= 10000; ++i) {
        double number = rand(); 
    if (i <= 10 || i >= 9991)
        results.push_back(number);
}

Upvotes: 0

Views: 205

Answers (1)

Marius Bancila
Marius Bancila

Reputation: 16328

state is your seed here. You initialize it with 1. After 10000 calls it will be 399268537. He gave you that value to check your implementation.

int main()
{
   for(int i = 0; i < 10000; ++i)
   {
      rand();
   }
}

If you call rand() again after this look and you step into the function and check the value of state you'll see it's 399268537.

Your code can me refactored as so that it looks much more like srand() and rand():

double seed(long state)
{
   g_state = state;
}

double rand(void)
{
   const long a = 48271;            
   const long b = 2147483647;      
   const long c = b / a;           
   const long r = b % a;           
   long t = a* (g_state % c) - r * (g_state / c);
   if(t > 0)
      g_state = t;
   else
      g_state = t + b;
   return ((double)g_state / b);
}

int main()
{
   seed(1);
   for(int i = 0; i < 10000; ++i)
   {
      rand();
   }
}

Upvotes: 1

Related Questions