Thread: simple question about function/random #'s

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    simple question about function/random #'s

    I'm creating a program that has a function that rolls a die to create a random number between 1-6. I have that figured out and running. The problem is that I need to call the function several times during the program, each with an individual result (randomly generated doubles are acceptable, though). These numbers need to be compared, too. The function is caleld up to 20 times, and i don't want to create 20 different variables within my main, all equal to the function output. Is there any way that I am able to do this?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Arrays, my friend.

    int my_random_numbers[20];
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    being the c++ newbie that I am, i don't understand how to use that. Also, the function is being called at dirrent times during the program, if that is of any relevance.

    -->edit: what i am going to expieriment with is using two functions, and just calling the both of them and comparing those (two dice are being rolled in the game, and when i call the function w/o assigning it to a local variable, the results are random)
    Last edited by jemer; 02-25-2004 at 08:29 PM.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Erm, sorry but you're not very clear about your question.
    Is the function a function you made yourself, or is it "rand()"?

    The problem is that I need to call the function several times during the program, each with an individual result
    I don't see the problem here... each time you get a random number, it gives you a different random number. Otherwise it wouldn't be very random...
    (randomly generated doubles are acceptable, though)
    Wha??? Isn't that the same thing??

    The function is caleld up to 20 times, and i don't want to create 20 different variables within my main
    Hmm, I think there's a misunderstanding... you can reuse variables.
    Code:
    #include <time.h>
    #include <iostream>
    
    int main()
    {
        srand(time(NULL));
    
        //Create 2 variables to hold the random numbers
        int randomNumber1, randomNumber2;
    
        for(int i = 0; i < 20; ++i)    //Loop 20 times
        {
            randomNumber1 = (rand() % 6) + 1;
            randomNumber2 = (rand() % 6) + 1;
    
            if(randomNumber1 == randomNumber2)
                std::cout << "Equal." << std::endl;
            else
                std::cout << "Not equal." << std::endl;
        }
        return 0;
    }
    I'm not 100% sure if the srand() line will work, I'm used to using GetTickCount() instead of time(NULL) since I do more Windows programs, but other than that line, the code should generate 20 pairs of random numbers and compare them for equality then quit.

    **EDIT** In case you meant you need to generate 20 random numbers and store ALL of them, then if you don't know how to use arrays, I suggest you learn to. In fact, I suggest you learn to use them regardless
    Last edited by Hunter2; 02-25-2004 at 08:48 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    I'll go more in-depth into the specifics of what my program is meant to do.

    -There are two players, and there are two dice.

    -player one, the user, rolls die 1. he them guesses if die 2 will get higher/lower/same as die 1. he rolls die #2, and an appropriate score is given

    -player 2, the AI, rolls die 1, and predicts what die 2 will be based on statistics entered by me (when i get ot that). die 2 is rolled and appropriate score is given.

    -for each correct guess, +1 point is given, and for each wrong guess, -1 point is given. the first to 10 wins. I have it working if the functions are only called once, because i assign them each to a local variable, and compare those. my problem with comparing the functions is that they always change, meaning the numbers put up on screen are not the one being compared by the computer (since a function is called once to display and a second time to compare).

    I hope you see now why I say it could gho up to 20, or even more, since each player needs to get 10 points to win.

    Wha??? Isn't that the same thing??
    What i meant in the first statement is not having all values as "1" or "2", etc. in the second statement, i was talking about numbers that appear as doubles randomly, which will happen, since more than 6 numbers are being generated.
    Last edited by jemer; 02-25-2004 at 09:38 PM.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Looks like you already understand the problem then
    But if you look at the code I gave you, you should be able to see the solution to your problem. Just do a loop until either player or computer has a score of 20, in which you store a random number in a variable, display the variable, store another random number, compare the 2, assign points, etc.

    I hope you've learned loops already??
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    Yes, i know how to use loops .

    i'll try what you suggested, this is greatly appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM