produce "random" numbers

Discussion in 'C++' started by chemr2, Feb 24, 2009.

  1. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    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:
     
    [LEFT]#include<iostream>
    #include<cmath>
    #include<ctime>
    using namespace std;[/LEFT]
     
    [LEFT]float randnumber();[/LEFT]
     
    [LEFT]int main()
    {
    float seed;
    char do_again = 'y';[/LEFT]
     
    [LEFT]cout<<"This program will produce 10 random numbers given a seed\n"<<endl;[/LEFT]
     
    [LEFT]randnumber();[/LEFT]
     
    [LEFT]cout<<seed<<endl;[/LEFT]
     
    [LEFT]cout<<"Would you like to receive another set of random number? Y or N?\n"<<endl;
    cin>>do_again;
    while (do_again == 'y')
    {
          randnumber();
    }[/LEFT]
     
     
    [LEFT]system("pause");
    return 0;[/LEFT]
     
    [LEFT]}[/LEFT]
     
    [LEFT]float randnumber()
    {
    float seed;
    float seed1;
    char prompt= 'y';[/LEFT]
     
    [LEFT]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;  
    } 
    }[/LEFT]
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    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;
        
    }
     
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > 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?
     
  5. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0

    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.
     
  6. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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".
     
  8. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  9. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    That's funny, because in your "Thanks. I have another question." post you actually posted "if (x%5 == 0 || x%2 == 0)".
     
  11. chemr2

    chemr2 New Member

    Joined:
    Feb 16, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Yes I was playing around with it and must have forgot to change it back. That was the last thing I would've thought to look at to be the problem.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice