Thread: randomizing?

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then argc == 4, argv[0] == "-cheats", argv[1] == "1", argv[2] == "-level", argv[3] == "99".
    Actually, the program itself is in there, so it's

    argc == 5, argv[0] == "myprog.exe", argv[1] == "-cheats", argv[2] == "1", argv[3] == "-level", argv[4] == "99".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Aaaaaaaagggggggggggg, When I read the FAQ I was totally lost. All your examples are great, but I still don't know how to incorporate them into my code or set the specific parameters for my program?
    My computer is awesome.

  3. #18
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) Using srand, seed the generator once.
    2) Determine the characteristics of the random numbers you need, and thus the corresponding parameters.
    3) Call rand with those paramaters.

    Now what specifically don't you understand?

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I don't understand this??
    Quote Originally Posted by sean_mackrory
    1) Using srand, seed the generator once.
    2) Determine the characteristics of the random numbers you need, and thus the corresponding parameters.
    3) Call rand with those paramaters.
    As I can see there are a lot of different ways of doing this and I got to here and I am stumped.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    srand(time(0));
    int x;
    int guess;
    
    do {
    cout << "im thinking of a number between 1 and 10\n";
    cout << "enter your guess please\n";
    
    cin >> guess;
    
    if (guess == x) {
    cout << "you are correct\n";
    }
    else if (guess < x) {
    cout << "guess a higher number\n";
    }
    else if (guess > x) {
    cout << "your not supposed to guess this high\n";
    }
    else {
    cout << "guess a lower number\n";
    }
    }while (guess != x);
    
    system("pause");
    return 0;
    }
    My computer is awesome.

  5. #20
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    First, indent your code. Second, if someones asks you questions in order to help you, answer them. I asked you what specifically you didn't understand. All you did was say, "I don't get this", and then post some code. What don't you understand? You haven't called ran() even once in your code. Have you tried it?

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Did you see I quoted you? I don't understand what you just said. I would call rand if I knew how?!
    My computer is awesome.

  7. #22
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    rand() returns a number from 0 to RAND_MAX. Therefore ((double)rand() / (double)(RAND_MAX + 1)) is a number in the range [0, 1). If you multiply this by the (integer) range of values you want (i.e. if you want a number from 2 to 5, the range is (5 - 2) + 1 = 4), then you get an integral value in the range [0, range). Now add the 'lowest' value that you want, in this case 2, and you'll have a value in the range of [2, 6). Since it's getting converted into an integer, the 6) gets truncated to 5], so you end up with a number in the range [2,5] as desired. So the formula is,
    Code:
    randomValue = (((double)rand() / (double)(RAND_MAX + 1)) * (highestValue - lowestValue + 1)) + lowestValue;
    Now you can plug that into your program and substitute your own 'parameters' or values for highestValue and lowestValue so that x is assigned a random number in the range that you want.

    Here's an example in code form:
    Code:
    int getRandomValue(int lowBound, int highBound)
    {
       //Find the number of possible values
       int range = highBound - lowBound + 1;
    
       //Get a random number from 0 to 1, excluding 1
       double random_multiplier = (double)rand() / (double)(RAND_MAX + 1);
       
       //Get a number from 0 to 'range', excluding 'range'
       int tempVal = (int)(random_multiplier * range);
    
       //Get a number from lowBound to highBound, including both
       int randomVal = tempVal + lowBound;
    
       return randomVal;
    }
    Hope this helps
    Last edited by Hunter2; 01-30-2005 at 10:09 PM.
    Just Google It. √

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

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know, if you had taken a few moments of your own to LOOK AT THE FAQ, you would have your C++ version.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    randomValue = (((double)rand() / (double) (RAND_MAX + 1)) * (10 - 1 +1)) + 1;
    Like that?
    My computer is awesome.

  10. #25
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, that will get you a number from 1 to 10. In case you didn't see, I edited my post just a second ago to include a code example that explains a little better what it all does, hopefully it's a little more comprehensible.

    >>When I read the FAQ I was totally lost.
    Admittedly, so was I until about 5 minutes ago It seems Prelude holds us all to a higher standard than many of us are able to cope with.
    Just Google It. √

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

  11. #26
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     int x = (((double)rand() / (double) (RAND_MAX + 1)) * (10 - 1 +1)) + 1;
     int guess
    
     do {
     cout << "im thinking of a number between 1 and 10\n";
     cout << "enter your guess please\n";
    
     cin >> guess;
    
     if (guess == x) {
     cout << "you are correct\n";
     }
     else if (guess < x) {
     cout << "guess a higher number\n";
     }
     else if (guess > x) {
     cout << "your not supposed to guess this high\n";
     }
     else {
     cout << "guess a lower number\n";
     }
     }while (guess != x);
    
     system("pause");
     return 0;
    }
    Is that where it goes?

    What now?
    My computer is awesome.

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is what where what goes?

    Your logic is wrong also. It should be:
    Code:
    if guess < 1
        tell them to guess a number between 1 and 10
    if guess > 10
        tell them to guess a number between 1 and 10
    if guess == randomly selected number
        tell them they won
    if guess < randomly selected number
        tell them to pick a larger number
    if guess > randomly selected number
        tell them to pick a smaller number
    On an aside, just so you stop asking: If your program works the way you think it should, then it's done. No need to keep asking if it's right if it does what it's supposed to.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Your logic is wrong also. It should be:
    Huh? Looks ok to me...

    >>int guess
    You're missing a semicolon at the end, but other than that it looks like it should run to me. And as quzah said, the quickest way to find out if it's done is to see if it works
    Just Google It. √

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

  14. #29
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    int x = (((double)rand() / (double) (RAND_MAX + 1)) * (10 - 1 +1)) + 1;
    If the rand stuff is supposed to go where it is. No, my program doesn't do what its supposed becuase it doesn't generate a random number yet. So what do I need to add now?
    My computer is awesome.

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Hunter2
    >>Your logic is wrong also. It should be:
    Huh? Looks ok to me...
    Look again:

    Code:
    if (guess == x) {
         cout << "you are correct\n";
    }
    else if (guess < x) {
         cout << "guess a higher number\n";
    }
    else if (guess > x) {
     cout << "your not supposed to guess this high\n";
     }
     else {
     cout << "guess a lower number\n";
     }
     }while (guess != x);
    Ok, perhaps the "logic" is fine. It's the wording that's wrong. I read it as the output states, that they've guessed an out-of-range number. Because you see, if the random number is say 5, and I guess six, then it will tell me "your(sic) not supposed to guess this high", to which I would reply. "Yes I am. You said between 1 and 10."

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomizing dealing program C
    By BSmith4740 in forum C Programming
    Replies: 2
    Last Post: 08-04-2008, 01:42 PM
  2. randomizing
    By zanderela in forum C Programming
    Replies: 2
    Last Post: 03-21-2008, 01:54 AM
  3. Randomizing link list.
    By Axel in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 10:03 AM
  4. srand() not randomizing me?!
    By crummy in forum C Programming
    Replies: 3
    Last Post: 02-11-2005, 07:23 PM
  5. Randomizing
    By Morph in forum C++ Programming
    Replies: 18
    Last Post: 01-26-2003, 06:36 PM