Thread: randomizing?

  1. #31
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I get what your saying I haven't changed it yet because I originally had x as 8 and some other stuff thats why it still like that.
    My computer is awesome.

  2. #32
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>It's the wording that's wrong.
    Oops. I just saw if(guess > x) and "guess" and "high"

    >>becuase it doesn't generate a random number yet.
    Sure it does. Well, you've forgot to seed the random-number generator by calling srand() at the top of main(), but what do you mean it doesn't generate a random number?
    Just Google It. √

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

  3. #33
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
     int x = (((double)rand() / (double) (RAND_MAX + 1)) * (10 - 1 +1)) + 1;
    [Warning] initilization from 'int' to 'double'
    [Warning] argument from 'int' to 'double'
    syntax error before do

    Could you post a lil code or explain to me how to call srand plz?
    My computer is awesome.

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about you just go read the FAQ, like you've already been told?

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

  5. #35
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    And, it's been posted at least twice on the first page of this thread.

    >> syntax error before do
    Yes, like I pointed out, you're missing a ';' after int guess.
    Just Google It. √

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

  6. #36
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    thanks, I already said I don't understand the FAQ.
    My computer is awesome.

  7. #37
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Read the
    FAQ
    Woop?

  8. #38
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Was there not a whole bunch of discussion about this just recently?
    My computer is awesome.

  9. #39
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    How you cannot read basic material and understand it?
    Yes it was discused but we still don't understand why you can't just read and comprehend simple material.
    Woop?

  10. #40
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by Hunter2TwoMinutesAgo
    And, it's been posted at least twice on the first page of this thread.

    >> syntax error before do
    Yes, like I pointed out, you're missing a ';' after int guess.
    Jeez. Read some posts before you talk.
    Just Google It. √

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

  11. #41
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright, so before I skimmed it and it looked complicated.
    This time I read it and believe I've made some progress.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     srand ( (unsigned int) time (0));
     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;
    }
    I know I haven't fixed everything else yet, but I added srand and the answer is 8 everytime I compile it.
    My computer is awesome.

  12. #42
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Its in plain sight guy your logic for rand is way off.
    Quote Originally Posted by FAQ
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
      std::srand ( (unsigned int)std::time ( 0 ) );
      std::cout<< std::rand() / ( RAND_MAX / 10 + 1 ) <<std::endl;
      std::cout<< (int)( (double)std::rand() / ( RAND_MAX + 1 ) * 10 ) <<std::endl;
    
      return 0;
    }
    This code shows two methods to use the high order bits of rand() by calculating the high value of the range with RAND_MAX and then dividing the call to rand() by the result. The point where 1 is added to RAND_MAX is important to note, this forces the range of random numbers to be 1 through 10 instead of 0 through 9. Notice that the expression is forced into a floating-point context and then back into integer context. The reason for this is that rand returns a number smaller than RAND_MAX, and division of a by b where b is a large value results in 0 for integral operations (remember that the fractional half is truncated). Now you know how to create random numbers of any range, simply change 1 and 10 to whatever you want the low number and high number of the range to be respectively.
    Woop?

  13. #43
    Banned
    Join Date
    Oct 2004
    Posts
    250
    I changed your code so it works i hope this gives you a general idea of how to generate a "random" number next time

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     srand ( (unsigned int) time (0));
     int x = rand()%5;
     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;
    }

  14. #44
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    cgod you never cease to amaze me.....
    Woop?

  15. #45
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Whats so bad about my answer? its not really good code but it helps him get started so he can understand better ways of doing it at lest it gets his program working.

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