Thread: What's better than using if() to tame rand()?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    What's better than using if() to tame rand()?

    Okay, folks, I've got my OOP back on track.
    Well I'm making a simple number guessing program into OOP, to make my head ache. And well, I decided to use rand(), (well actually rand() % 100 as suggested by an article) and if() to tame it so it can fit between 1 and 100. Here is the code:

    Code:
    void CNumber::getRand()
    {
    	int randNum;
    	srand(time(NULL));
    	randNum = rand() % 100;
    	if ((randNum < 1) || (randNum > 100))
    	{
    		int equalRand(randNum);
    	}
    	else
    	{
    		getRand();
    	}
    }
    Yes, I know. Everyone hates recursive functions. And in a sense this could go on for days. But the program loads and then crashes. Because of this (I just added this bit, and now it crashes.).

    There must be a better way of doing this. What is it?

    Thanks.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>if ((randNum < 1) || (randNum > 100))
    Switch up those greater than and less than signs. Otherwise you'll get an infinite loop and run outta stack space.

    >>int equalRand(randNum);
    Why does this even compile?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Maybe I'm misunderstanding what you are trying to do, but didn't you mean to write:

    Code:
    if ((randNum > 1) && (randNum < 100))
    ...

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Heh. That's the first thing I pointed out in my post.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    The code I showed was just a snippet, you could've also noticed that I never declared CNumber, as I didn't post it (I did really, but just didn't post it).

    Okay, so I switched up the || and < >(it's now && and > <). But now it's telling me the number is less than 0. Every time.

    O_o
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    rand( ) % 100 will return a value 0-99, so you want to add 1. Also, use >= and <=.

    Edit:
    My postcount is now 'leet'!!!!!
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    It still says that 0 is too high. So it's either a negative number, or a number that doesn't exist.

    Gosh. Isn't there a better way?
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by XSquared


    >>int equalRand(randNum);
    Why does this even compile?
    That is a great question -- it's halfway between a prototype and a function call -- the int prevents it from being legal syntax for a call, and the lack of a type specified for randNum prevents it from being legal syntax for a prototype. It should NOT compile.
    Last edited by Cat; 08-27-2003 at 01:04 AM.

  9. #9
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I've noticed MSVC 6.0 will compile functions with no return type. I guess it assumes void.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  10. #10
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by Cat
    That is a great question -- it's halfway between a prototype and a function call -- the int prevents it from being legal syntax for a call, and the lack of a type specified for randNum prevents it from being legal syntax for a prototype. It should NOT compile.
    Whats wrong with it? It initializes the equalRand integer with the value of the randNum integer.

    You know, like...

    int::int(int)

    if it were an object


    edit: is rand()? bugged that it sometimes returns outbound random numbers or why does he test the results?
    Last edited by darksaidin; 08-27-2003 at 05:07 AM.
    [code]

    your code here....

    [/code]

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> why does he test the results?

    As salem pointed out before, his "taming" is completely superfluous.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by darksaidin
    Whats wrong with it? It initializes the equalRand integer with the value of the randNum integer.
    OK, I don't know what is worse, pretending I didn't know that or admitting I forgot.

    Lol, I guess my excuse will be that I posted that at 2:30 AM

    You're completely correct, it is a legal statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand() technical question
    By freakme in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 10:22 PM