Thread: Randomizing

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Question Randomizing

    I'd like to make a program that chooses randomly. Say, either Potato or Apple. To do this I figured I'd choose randomly from the numbers 1 to 10, and then decide which of the vegetables I'd choose. 1-5 = Apple, 6-10 = Potato. How do I do this? I know its a piece of cake for you experienced programmers...

    regards,
    Morph

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    First you seed the random number generator so that the next time you run the program it's not the same, then just use the rand() function.
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      srand(time(0));
    
      int r = rand() % 10 + 1;
      cout<<"Random number -- "<< r <<endl;
    }
    *Cela*

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    6
    It seems I'm missing a couple of the library files. I'm using Borland C++ 5, and it can't find the cstdlib.h and ctime.h files.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I'm using Borland C++ 5, and it can't find the cstdlib.h and ctime.h files.
    If you're using them with the .h at the end then it won't work, remove the extension and it should compile fine. If it doesn't then change them to stdlib.h and time.h.
    *Cela*

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    6
    It works now. Thanks a lot Cela

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you only want your program to choose between two random values, you need only do this:
    Code:
    rand() % 2;
    That will evaluate to either zero or one. That should save you a little room and time.
    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.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>That will evaluate to either zero or one.
    Yea, but how random will it be? In my experience not very, that's why if I need a random value between 0 and 1 I do this :-)
    Code:
    (rand() < RAND_MAX/2) ? 0 : 1;
    *Cela*

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    They should both be just as random. The only reason why yours would possibly be better would be if for some reason the random number generated more odds than evens or vice versa. I've never had a problem with that and all the tests I've done where it gets millions of numbers has always shown a 50/50 spread.

  9. #9
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >> I've never had a problem with that
    It works for you, that's good. It works well on one compiler I use too, but only that one. Read the link Salem posted, just because your generator is good enough to handle rand() % 2 well doesn't mean everyone else who uses your code will be as lucky. You're not just programming for yourself, but for other people to, so be nice to them and program portably. :-)
    *Cela*

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ahhhh... that makes sense

  11. #11
    c++wizard
    Guest

    Talking

    if you want it to randomize every time you run it you need to put a
    "randomize();"
    function in it or it i think will come up with the same random every time it is runned

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    The alpha way.

    Code:
    #ifndef _MAIN_CPP_
    #define _MAIN_CPP_
    
    // C++ Includes
    #include <iostream>
    
    // C Includes
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    using std::cout;
    using std::endl;
    
    char* Fruit(void);         // returns a string
    
    int main(int argc, char **argv)
    {
     cout << "Today you'll be eating " << Fruit() << endl;
    
     return( cin.get() ? EXIT_SUCCESS:EXIT_FAILURE);
    }
    
    char* Fruit(void)
    {
     srand( (int)time(0) );
     char* lpFruit;
     int result=1 + (rand() % 5);
    
     if(result== 1) { lpFruit= "Apples"; }
     else if(result== 2) { lpFruit= "Grapes"; }
     else if(result== 3) { lpFruit= "Banana"; }
     else if(result== 4) { lpFruit= "Peach"; }
     else if(result== 5) { lpFruit= "Urine"; }
    
     return(lpFruit);
    }
    
    #endif

  13. #13
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Why does the rand() function work differently on different compilers or computers? Shouldn't it be reliable if it's part of the standard library? I read the posted link, but it didn't provide as much information on the subject as I wanted. Please explain the discrepancy.
    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.

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Random numbers are a ***** to get right, and you can't expect everyone to use the best algorithm. rand is standard and every implementation meets the requirements, but that doesn't mean they're all the same.

  15. #15
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Crimpy
    Random numbers are a ***** to get right, and you can't expect everyone to use the best algorithm. rand is standard and every implementation meets the requirements, but that doesn't mean they're all the same.
    Why doesn't the standard require everyone to use one algorithm that is determined the best? What are the current standards on the subject, and why is the current standard not uniform? I always thought the point of having a standard was to make things standard.
    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.

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 cerin in forum C++ Programming
    Replies: 55
    Last Post: 02-03-2005, 06:58 PM