assigning rand outputs to variables

This is a discussion on assigning rand outputs to variables within the C++ Programming forums, part of the General Programming Boards category; well...im trying to assign the outputs generated by rand () to a variable so that i can multiply them later. ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    assigning rand outputs to variables

    well...im trying to assign the outputs generated by rand () to a variable so that i can multiply them later. unfortunately, i keep getting 'intializing' errors regardless of whether i try to just assign the value to a variable, or try to put the values into an array. any ideas (ive been searching through threads for about an hour!)?

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Posting some code would be nice...
    Don't forget code tags...

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    98

    Question

    every problem can be fond in msdn :
    this is it's give out example about rand() using method.

    Example
    Code:
    // crt_rand.c
    /* This program seeds the random-number generator
     * with the time, then displays 10 random integers.
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main( void )
    {
       int i;
    
       /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
        */
       srand( (unsigned)time( NULL ) );
    
       /* Display 10 numbers. */
       for( i = 0;   i < 10;i++ )
          printf( "  %6d\n", rand() );
    }
    [EDIT]:
    why this example main() no " return 0 ; " statement in the end ?
    Last edited by toysoldier; 09-27-2004 at 08:03 PM.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by toysoldier
    every problem can be fond in msdn :
    this is it's give out example about rand() using method.
    how bout posting the code that your having trouble with, not a working example from msdn.

    why this example main() no " return 0 ; " statement in the end ?
    Its good practice to put "return 0;" in yourself, but some compilers will exit with 0 if you dont specify anything. It is inlined at compile time.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    ok here goes...

    Code:
    #include <iostream> 
    #include <cmath> 
    #include<cstdlib> 
    #include<ctime>
    
    using namespace std;
    
    int random(int low, int high) 
    { 
       return low+rand()%(high-low+1); 
    } 
    
    int random2(int low, int high) 
    { 
       return low+rand()%(high-low+1); 
    } 
    
    int main ()
    {
        int answer;
        srand (time(0));
        cout << "How much is ";
           cout << random(0,10);
        
        cout << " times ";
           cout << random2(0,10);
        cout << "?\n";
        cin >> answer;
        int c [2] = {random, random2};
            
        int total = 0;
        for (int i = 0; i < 2; i ++)
            total *= a[2];
        if ( answer == total)
            cout << "correct\n";
        else 
            cout << "try again\n";    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    int c [2] = {random, random2};


    You never said what random or random2 is. You have functions named random and random2, but no variables. Right there it is looking for variables, not returns from the function. The return value falls out of scope and dies right after the function exits, so you need to catch it in a real variable before trying to access it.

    Code:
    int randreturned = random(0,10);
    cout >> randreturned;
    Repeat for the second value.
    then when you are trying to put it into c...

    Code:
     int c[2] = {randreturn, randreturn2};
    Last edited by Lifedragn; 09-27-2004 at 09:41 PM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    oh i see...create an integer then assign the function return to it, then instead of displaying the return, display the new integer. blasted function calls...

    thanks!

    edit: well it compiles now, but the only answers it gets right are the 'n * 0' questions...im gonna see if itll work without using an array
    Last edited by 2fastwrx; 09-27-2004 at 10:12 PM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    Hope that solved things.

    Just remember in the future. declaring a variable inside of a function creates what is called a local variable. As soon as the function exits, it is drug out back and shot on the spot. Everytime you call a function, it recreates the variables within, and everytime it exits it kills them. variable = function(parameters) is the only way to keep a return, or else it dies on the next line itself.
    Last edited by Lifedragn; 09-27-2004 at 10:14 PM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    yup that fixed it...guess i had the code wrong cause the array wont multiply right, so i just did this
    Code:
     int total = randreturn * randreturn2;
        if (answer == total)
            cout << "correct\n";
        else 
            cout << "try again\n";
    and it works...i have a few more features that need to be put in, but hopefully i'll figure them out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Assigning variables to strings
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2006, 06:39 PM
  5. i/o assigning variables
    By bballzone in forum C++ Programming
    Replies: 4
    Last Post: 07-30-2004, 10:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21