Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   produce "random" numbers (http://www.go4expert.com/forums/produce-random-numbers-t16312/)

chemr2 24Feb2009 07:44

produce "random" numbers
 
Hello I just started programming about a month or so ago. I am doing a program that will produce "random" numbers. I am confused on how I need to do this. I have written some code that will follow(some of the variables declared aren't being used I was just experimenting). Any help to guide me in the right direction is appreciated. The problem states: Using the power residue method,

a) Create a function randnum() that accepts a floating point "seed" as a parameter and returns a floating-point random number between 0 and 1.e6.

b) Incorporate the randnum() function created in 7a into a working program that produces 10 random numbers between 0 and 1.e6.

Code:


#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;

float randnumber();

int main()
{
float seed;
char do_again = 'y';

cout<<"This program will produce 10 random numbers given a seed\n"<<endl;

randnumber();

cout<<seed<<endl;

cout<<"Would you like to receive another set of random number? Y or N?\n"<<endl;
cin>>do_again;
while (do_again == 'y')
{
      randnumber();
}

 
system("pause");
return 0;

}

float randnumber()
{
float seed;
float seed1;
char prompt= 'y';

while (prompt == 'y')

cout<<"Please enter an odd six-digit integer that is non-divisible by two or five...\n"<<endl;
cin>>seed;
seed = seed * 997;
seed = seed/1000000;
seed1 = seed * 1000000;
seed = seed - seed1;
return seed; 
}
}


xpi0t0s 24Feb2009 13:57

Re: produce "random" numbers
 
The way pseudorandom numbers generally work is to modify the seed according to some formula then use the new value as the next seed. So:
initialise seed <- this is the number you enter
random number = f(seed) <- this function returns a random number as a function of seed
random number 2 = f(random number) <- so the result of the previous is fed into the function again.

So you need to keep seed somewhere instead of discarding it, so I would suggest making the "please enter" part a separate part of randnum[ber]() and pass the seed into this function.

A for loop will do the "display 10 random numbers" bit.

chemr2 25Feb2009 04:52

Re: produce "random" numbers
 
Quote:

Originally Posted by xpi0t0s (Post 43372)
The way pseudorandom numbers generally work is to modify the seed according to some formula then use the new value as the next seed. So:
initialise seed <- this is the number you enter
random number = f(seed) <- this function returns a random number as a function of seed
random number 2 = f(random number) <- so the result of the previous is fed into the function again.

So you need to keep seed somewhere instead of discarding it, so I would suggest making the "please enter" part a separate part of randnum[ber]() and pass the seed into this function.

A for loop will do the "display 10 random numbers" bit.

Thanks. I have another question. I have to make sure that the number entered "the seed" is not divisible by 2 or 5. I have an error check in my program but it seems even if it is not divisible it still produces the results. I have posted a copy of my updated code.

Code:

#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;
 
double randnum(double);  //function prototype
 
int main()
{
   
    int x;
    double i;
   
    cout<<"This program will produce 10 random numbers given an inital seed\n"<<endl;
 
    cout<<"Please enter the 6 digit seed value non-divisible by 5 or 2\n"<<endl;
    cin>>x;
   
 if (x%5 == 0 || x%2 == 0)
    {
      cout<<"The number entered is not a valid entry"<<endl;
      system("pause");
      return 0;
    }
   
   
else
    {
        cout<<"\nThe random numbers are as follows\n"<<endl;
         
          for(i=1.;i<=10.;i++)          //increments the random numbers
              {
                  x = randnum(x);
                  cout<<x<<endl;
              }
   
    }
system("pause");
return 0;

 
//The function that will produce the random numbers
double randnum(double x)
{
    double i;
   
      i = int(997.0 * x/ 1000000.);
      x = 997.0 * x - i * 1000000.;
 
    return x;
   
}


xpi0t0s 25Feb2009 05:08

Re: produce "random" numbers
 
> even if it is not divisible it still produces the results

I thought it was supposed to produce results if it is not divisible. It's if it IS divisible that the number isn't valid.
What number did you enter?
After cin>>x you could try displaying the number entered as a check that x contains what you're expecting.

Why is your loop variable i a double?

chemr2 25Feb2009 05:16

Re: produce "random" numbers
 
Quote:

Originally Posted by xpi0t0s (Post 43399)
> even if it is not divisible it still produces the results

I thought it was supposed to produce results if it is not divisible. It's if it IS divisible that the number isn't valid.
What number did you enter?
After cin>>x you could try displaying the number entered as a check that x contains what you're expecting.

Why is your loop variable i a double?


O yes you are correct. It's one of those things where you know what you meant but typed it wrong. i should be int, I don't know why I had double. An example number I entered would be 346555. If it IS divisible then I want the program to tell the user the number is not valid and end. If it is NOT divisible then I want the program to continue and loop out the ten random numbers.

chemr2 25Feb2009 05:39

Re: produce "random" numbers
 
Also, I tried numbers such as: 9000 900000 500000 345000..All of these numbers will produce an error. If I type 345555 this number will pass through and produce the random numbers, even though it is divisible by 5. I'm a little confused as to why this is happening.

xpi0t0s 25Feb2009 06:24

Re: produce "random" numbers
 
Don't know why 345555 passes through, it is certainly divisible by 5. Compiler bug perhaps? What compiler are you using?
Did you try printing the number entered back on screen to check the variable contains the expected value?
Paste the following function into your code, call it, and let me know what the output was:
Code:

void go4e_43398()
{
        int num[7]={3,7,15,20,345555,36,40};
        for (int i=0; i<7; i++)
        {
                if (num[i]%5==0 || num[i]%2==0)
                        printf("%d is divisible by 2 or 5\n",num[i]);
        }
        printf("sizeof int=%d\n",sizeof(int));
}

Obviously this shouldn't say anything for 3 or 7, but the rest should print "is divisible by 2 or 5".

chemr2 25Feb2009 06:35

Re: produce "random" numbers
 
Quote:

Originally Posted by xpi0t0s (Post 43406)
Don't know why 345555 passes through, it is certainly divisible by 5. Compiler bug perhaps? What compiler are you using?
Did you try printing the number entered back on screen to check the variable contains the expected value?
Paste the following function into your code, call it, and let me know what the output was:
Code:

void go4e_43398()
{
    int num[7]={3,7,15,20,345555,36,40};
    for (int i=0; i<7; i++)
    {
        if (num[i]%5==0 || num[i]%2==0)
            printf("%d is divisible by 2 or 5\n",num[i]);
    }
    printf("sizeof int=%d\n",sizeof(int));
}

Obviously this shouldn't say anything for 3 or 7, but the rest should print "is divisible by 2 or 5".

I am using Dev C++. I did print x after the cin>> and it was the same value so obviously there isn't a problem with that. After calling the function you gave me the results were:

15 is divisible by 2 or 5
20 is divisible by 2 or 5
345555 is divisible by 2 or 5 (hmmmmmm why is it working here?)
36 is divisible by 2 or 5
40 is divisible by 2 or 5
size of int =4

chemr2 25Feb2009 07:01

Re: produce "random" numbers
 
Quote:

Originally Posted by chemr2 (Post 43407)
I am using Dev C++. I did print x after the cin>> and it was the same value so obviously there isn't a problem with that. After calling the function you gave me the results were:

15 is divisible by 2 or 5
20 is divisible by 2 or 5
345555 is divisible by 2 or 5 (hmmmmmm why is it working here?)
36 is divisible by 2 or 5
40 is divisible by 2 or 5
size of int =4

I have figured out the problem I think. In my code I had this
(x%5 == 0 && x%2 == 0)
Obviously 345555 is divisible by 5 but is not divisible by 2, 9000 900000 and all of those numbers are divisible by both. That is why the number was not passing through. I changed it to
if (x%5 == 0 || x%2 == 0)
now it can be either or.

xpi0t0s 25Feb2009 13:10

Re: produce "random" numbers
 
That's funny, because in your "Thanks. I have another question." post you actually posted "if (x%5 == 0 || x%2 == 0)".


All times are GMT +5.5. The time now is 02:30.