Thread: randomizer

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    randomizer

    I've been looking around and trying to get a random number generator easy right? Not really I guess. Here is what I have but I'm getting an error "undefined reference to randomize"

    Code:
    int deal(void)
    {
        int random;
        randomize();
        random = rand() % 11;
        
        return random;
    }
    Also have a question about strcmp, can you compare against a string like this:

    Code:
    while(strcmp(choice, 'quit') == 0);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    1) The function you're looking for is srand(). Hint: You can use time(0) as an adequate seed in most cases.

    2) You need to use double quotes, not single.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Minor nit-pick. I believe it's time( NULL ) for C, and time( 0 ) for C++. Or does it make no difference?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by msh View Post
    Minor nit-pick. I believe it's time( NULL ) for C, and time( 0 ) for C++. Or does it make no difference?
    No difference. In both C and C++, a pointer with value zero is equivalent to the NULL pointer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >randomize();
    >random = rand() % 11;
    Assuming you replace randomize with srand, it's still not going to work properly. The generator should be seeded very rarely, if more than once. Seeding every time you call rand will produce rather nonrandom numbers.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    Additional little nit pick: using the modulo operator to shrink the range of rand() is okay for a simple program where uniformity is of no concern, but the results of such randomization are skewed. Using a pseudorandom deviate (as explained in this informative page) is a better solution. Basically, you produce a pseudorandom deviate between 0 and 1 by calling rand() and dividing it by the number of different results it offers (one more than RAND_MAX), then you multiply that by the number of results you want.

    Code:
    int deal(void)
    {
        int random;
        random = rand() * (1.0 / (RAND_MAX + 1.0)) * 11;
        return random;
    }

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Prelude View Post
    >randomize();
    >random = rand() % 11;
    Assuming you replace randomize with srand, it's still not going to work properly. The generator should be seeded very rarely, if more than once. Seeding every time you call rand will produce rather nonrandom numbers.
    I did some (very) basic testing on this, and if the application runs at user-speed, e.g. there is a delay for user input between each srand() call, the results are satisfactory. If the application runs at computer-speeds, however, it's pretty much broken.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If the application runs at computer-speeds, however, it's pretty much broken.
    The time function works with second granularity. If your user interface is clunky enough that users are physically unable to make a request without waiting at least one second, calling srand immediately before every call to rand should work.

    However, I still see no reason why you would want to do it this way. The period of the random number generator should be more than enough to satisfy your randomization needs with a single call to srand. Further, you'd be added unnecessary overhead by calling srand when it doesn't need to be called.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomizer
    By thescratchy in forum C Programming
    Replies: 2
    Last Post: 02-26-2010, 05:15 PM
  2. randomizer
    By barneygumble742 in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2005, 08:06 AM
  3. Randomizer for CounterStrike - help
    By cj348879 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2002, 04:37 PM