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

1. ## 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.

2. >>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?

3. Maybe I'm misunderstanding what you are trying to do, but didn't you mean to write:

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

4. Heh. That's the first thing I pointed out in my post.

5. 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

6. rand( ) % 100 will return a value 0-99, so you want to add 1. Also, use >= and <=.

Edit:
My postcount is now 'leet'!!!!!

7. 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?

8. 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.

9. I've noticed MSVC 6.0 will compile functions with no return type. I guess it assumes void.

10. 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?

11. >>> why does he test the results?

As salem pointed out before, his "taming" is completely superfluous.

12. 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.