• 11
• 9
• 11
• 9
• 11

# function for randomizing variables

This topic is 3550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

*//SOLVED Im pretty new at some things in C++ and this happens to be one of those things that I don't have down yet. I'm sure this is a super easy problem. What I need is a function asks for a max and min for the random range then makes a random variable and returns it. This is what I have so far:
void RandValue(int rand_min, int rand_max)
{
int rand_current;

srand(time(NULL));
rand_current = rand() % rand_max+rand_min;
}
Now this all works well except now I need to be able to use this function like this:
cord_x=RandValue(1,100);
What do I need to add to do this? I tried return(rand_current) but that does work. Anyone know? Thanks [Edited by - snowfell on August 3, 2008 1:53:37 PM]

##### Share on other sites
return isn't a function call, so you don't need the parenthesss:
return rand_current;

Additionally you should probably not have the call to srand within this function; it seeds the random number generator, and you most likely only want to call it once at the beginning of your program (or at least some time before trying to make use of rand.

//EDIT: You also need to specify an appopriate return type for your function.

##### Share on other sites
Ok I changed the code a bit but when I tryed using the code like I have posted above I got a error like "void value not ignored as it ought to be". Any idea why it would say this?

What function currently looks like:
void RandValue(int rand_min, int rand_max){        int rand_current          srand(time(NULL));     rand_current = rand() % rand_max+rand_min;          return rand_current;}

EDIT: "You also need to specify an appopriate return type for your function." What exactly does this mean?

##### Share on other sites
You need to give your function a return type.

int RandValue(int rand_min, int rand_max){	int rand_current;		rand_current = rand() % (rand_max - rand_min);		return rand_current + rand_min;}

##### Share on other sites
I think got it now. Thanks guys

##### Share on other sites
Quote:
 Original post by snowfellvoid RandValue(int rand_min, int rand_max){ int rand_current srand(time(NULL)); rand_current = rand() % rand_max+rand_min; return rand_current;}

Try to print 10 random numbers and see what happens. It has to do with srand and time.

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by snowfellvoid RandValue(int rand_min, int rand_max){ int rand_current srand(time(NULL)); rand_current = rand() % rand_max+rand_min; return rand_current;}

Try to print 10 random numbers and see what happens. It has to do with srand and time.

That's one problem in this function. Hint: Random numbers aren't really random. They're simply a math function which returns the same sequence of numbers every time, based off the seed you pass in for the original number - srand() sets the seed value with time.

Another problem is, rand()%rand_max + rand_min isn't going to be random numbers from rand_min to rand_max. It's going to be random numbers from rand_min to (rand_max + rand_min - 1). Think about why. The modulo operator essentially causes a "wrap around" due to the way it works. So rand()%rand_max will give you 0..rand_max-1. When you add rand_min then to that value, you get rand_min..(rand_max-1+rand_min). You need to compensate for that.

To fix this, you need to do rand() % (rand_max - rand_min + 1) + rand_min. This will return random numbers from rand_min to rand_max.

##### Share on other sites
Quote:
 Original post by snowfellEDIT: "You also need to specify an appopriate return type for your function." What exactly does this mean?

"Specify" means "say exactly what something should be".
"appropriate", here, means "matching what you want it to be".
A "return type" is the type of the thing that the function will return. A "type", roughly speaking, is the kind of thing that something is.

In C++, the return type of a function is specified by the type name that appears right before the function name. The word "void" isn't some kind of programmer talk for "function"; it's the name of a type. (Well, actually, it's the name of the lack of a type - this is a special case: when we say that a function should "return something of type 'void'", what we really mean is that it shouldn't return anything at all.) So, we need to replace that with the "appropriate" type - the type of the thing we want to return. We're going to return 'rand_current', which is an int, so our return type is 'int'.

However, we don't actually need to set up a variable here; in fact, it only makes things needlessly cluttered. In C++, as in pretty much every programming language ever that has the concept of "returning", we can return the value of any expression, not just a variable. Thus, we simply 'return rand() % rand_max + rand_min'.