Link to home
Start Free TrialLog in
Avatar of jasonbrandt3
jasonbrandt3

asked on

Need help with random number generation?

I working on a C tutorial where I need to write a statement to assign random integers to a variable in a range, such as 1 <= n <=2 or -3 <= n <= 11, ect...  I understand I need to use the rand function, but I'm unclear on creating a range for it, hope this makes sense!
Avatar of Infinity08
Infinity08
Flag of Belgium image

The rand() function generates a value between 0 and RAND_MAX (inclusive). All you have to do, is linearly transform that range into your range.

        http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

To transform a range [A, B] to a range [C, D], this equation should help :

        (x - A) / (B - A) = (y - C) / (C - D)

where x is a value in the range [A, B], and y is the corresponding value in the range [C, D]. You can easily get this formula out of it for y :

        y = C + ((C - D) * (x - A) / (B - A))

or with the values for rand() :

        A = 0
        B = RAND_MAX
        y = C + ((C - D) * x / RAND_MAX)


Take a look at the example code on the page I posted, where a value in the range [1, 10] is generated.
>>         y = C + ((C - D) * x / RAND_MAX)

Note that this is not C++ code - it's a mathematical equation. To make it work correctly in C++, you'll need to add some casts and/or re-arrange the code a bit. I refer to the reference page for the rand() function I posted.
Avatar of jasonbrandt3
jasonbrandt3

ASKER

Is this close?
a) 1 <= n <= 2
 int  n = rand() % (2 -1);
b) 1 <= n <= 100
int  n = rand() % 100 +1;
c) 0 <= n <= 9
int n = rand() % 9
d) 1000 <= n <=1112
int n = rand() %(1112) + 1000;
e) -1 <= n <= 1
int n = rand() %(1 )  1;
f) -3 <= n <=11
int n = rand() %(11) -3;
>> a) 1 <= n <= 2
>>  int  n = rand() % (2 -1);

Close. Take a look at your solution for b)

>> b) 1 <= n <= 100
>> int  n = rand() % 100 +1;

Looks good.

>> c) 0 <= n <= 9
>> int n = rand() % 9

Almost. x % y results in a value between 0 and (y-1) or the range [0, y[. So, y is NOT included ...

>> d) 1000 <= n <=1112
>> int n = rand() %(1112) + 1000;

rand() %(1112) will result in a value between 0 and 1111 (inclusive). Adding 1000 to that will result in a value between 1000 and 2111 ... That's not exactly what you want ;)

>> e) -1 <= n <= 1
>> int n = rand() %(1 )  1;

How many possible values for n are there ? Keep that in mind when determining what right operand for the modulo (%) operator to use.

>> f) -3 <= n <=11
>> int n = rand() %(11) -3;

Same comment as for e)
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I understand now!!!  Thanks!
Feel free to post your solutions, and I'll have a look at them.
ok I will this evening.  Thanks again.
Kinda confused on the last two....
a) 1 <= n <= 2
>>  int  n = rand() % (2) - 1;

b) 1 <= n <= 100
int  n = rand() % 100 +1;

c) 0 <= n <= 9
 int n = rand() % (9) - 1;

d) 1000 <= n <=1112
int n = rand() %(1112) + 1;

e) -1 <= n <= 1
int n = rand() %(-1 ) - 2;

f) -3 <= n <=11
int n = rand() %(-3) + 11;





>> a) 1 <= n <= 2
>> >>  int  n = rand() % (2) - 1;

Almost - there's a small problem. Look closely to how you solved b)


>> b) 1 <= n <= 100
>> int  n = rand() % 100 +1;

Still good :)


>> c) 0 <= n <= 9
>>  int n = rand() % (9) - 1;

No. A modulo operation gets the rest after an integer division. modulo 9 (% 9) means the rest after dividing by 9. The result is a value between 0 and 8 (inclusive), so either 0, 1, 2, 3, 4, 5, 6, 7 or 8. Not 9. You're then subtracting 1, so the final result will be in the range -1 <= n <= 7 which is not the range that you want.

Note that there are 10 values in the range 0 <= n <= 9, namely 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.


>> d) 1000 <= n <=1112
>> int n = rand() %(1112) + 1;

Not quite. I'll let you figure this one out ... try to find out which possible values you can get out of this :

        int n = rand() %(1112) + 1;

(do it similarly to my explanation for c))


>> e) -1 <= n <= 1
>> int n = rand() %(-1 ) - 2;

modulo doesn't work very well with negative numbers. The modulo is done to limit the amount of possible results (ie. the width of the range). The addition afterwards depends on the first value in the target range. Think about it a bit ...


>> f) -3 <= n <=11
>> int n = rand() %(-3) + 11;

Same comment as for e)
last two again:

e) -1 <= n <= 1
int n = rand() %(1 ) - 2;

f) -3 <= n <=11
int n = rand() %(11) -5;
a) 1 <= n <= 2
int  n = rand() % (2) +1;


c) 0 <= n <= 9
int n = rand() % (9) +1;

d) 1000 <= n <=1112
int n = rand() %(1000) + 112;





>> last two again:

No ... Just take it slowly. Try to understand how this is working.

Let's analyze your solution for e) :

        int n = rand() %(1 ) - 2;

rand() returns a value between 0 and RAND_MAX (inclusive).
rand() % 1 will always return 0, because every value is divisible by 1, so there's never a remainder.
so, rand() %(1 ) - 2 will always be -2.

You might as well have written :

        int n = -2;

;)

Let me highlight a comment I made in my previous post :

>> The modulo is done to limit the amount of possible results (ie. the width of the range). The addition afterwards depends on the first value in the target range. Think about it a bit ...

Let it sink in a bit ... Do you understand what I mean ? If not, feel free to ask for clarification.
This is the basis of all these exercises, so you need to understand this.
>> a) 1 <= n <= 2
>> int  n = rand() % (2) +1;

That's it. Correct.


>> c) 0 <= n <= 9
>> int n = rand() % (9) +1;

That would get you the range 1 <= n <= 9. See my last comments in my previous post before continuing ...


>> d) 1000 <= n <=1112
>> int n = rand() %(1000) + 112;

I think you're just guessing now. That's never a good idea. Try to understand how this works.
What you have now would give you the range 112 <= n <= 1111
e)int n = rand() %(-1 ) + 2;
>> e)int n = rand() %(-1 ) + 2;

As I said, modulo operations don't work very well with negative numbers ... ;)

Please, take a moment to consider my explanations earlier. Understand them first before continuing. Whatever is not clear, ask me about it, and I'll be glad to explain/clarify. Unless you understand it, there's no sense in continuing ;)
d) 1000 <= n <=1112
 int n = rand() %(1112) -112;
e) -1 <= n <= 1
int n = rand() %(2 ) - 2;
jasonbrandt3, can you please consider my comments ?
Neither your latest solutions for d) and e) are correct. You really need to try to understand what I explained instead of continuing to guess. You're not learning anything this way.
c) 0 <= n <= 9
int n = rand() % (10) -1;

I feel like I'm close...
Come on, jasonbrandt3. I feel like I'm wasting my time. Do you actually read what I write ?
maybe I'm not understanding the mod...so %9 is the remainder of  going to give me a set betweein 0 and 8, so if I add 1 I would get 1<=n<=9??  I'm not sure if I'm getting the modulus..
>> I'm not sure if I'm getting the modulus..

Now we're getting somewhere.

Ok, let me explain.

An example of a normal integer division :

        9 / 4 = 2

9 divided by 4 is 2. Since it's integer division, the result has to be an integer (otherwise it would have been 2.25).

Now, the modulo operation for the same values is :

        9 % 4 = 1

meaning that the remainder after dividing 9 by 4 is 1, or in other words :

        9 = (2 * 4) + 1

The + 1 is the remainder.

Now, let's take some general values :

        z = x % y

z is the remainder after dividing x by y.

z will always be smaller than y. And it will always be positive, so its range is :

        0 <= z <= y-1

Now, in these exercises, we use the modulo operation to limit the width of the range (or to scale down the range if you wish), so :

        0 <= rand()           <= RAND_MAX
        0 <= (rand() % x) <= x-1

Does that make more sense ?
c) 0 <= n <= 9
int n = rand() % (9) + 1;

Is that right?  I'm getting the values 0-8 and need to add one.

>> c) 0 <= n <= 9
>> int n = rand() % (9) + 1;

You've already tried this one.

>> Is that right?  I'm getting the values 0-8 and need to add one.
So 0-8 plus one would give 1-9, wouldn't it ? Is that what you want ?


Did you understand my explanation of the modulo operator ? Do not hesitate to ask whenever something is not clear.
No.  I think I'm overthinking this.
%(10)
would yield 0 and 9, value of 0 through 9.
So I don't have to always add or subtract, the mod by itself sometimes yields my range?
>> No.  I think I'm overthinking this.
>> %(10)
>> would yield 0 and 9, value of 0 through 9.

Precisely.


>> So I don't have to always add or subtract, the mod by itself sometimes yields my range?

Good :)
>> the mod by itself sometimes yields my range?

That "sometimes" is when the lower limit of the range is 0 btw ;)
The reason for this is that the lower limit of the range produced by rand() is also 0.
I had a brain block!!! sorry..
c) 0 <= n <= 9
int n = rand() % (10);

e) -1 <= n <= 1
int n = rand() %(2 ) - 1;

I hope I got this:)
>>  I had a brain block!!! sorry..

No problem. If something's not working, just take it slowly, and understand what's going on, and it will come to you :)

Now, let's get back to the exercises. Can you solve them now ?
> The reason for this is that the lower limit of the range produced by rand() is also 0.
I'd say that the reason is that the lower limit of the range produced by mod is 0
>> c) 0 <= n <= 9
>> int n = rand() % (10);

Good.


>> e) -1 <= n <= 1
>> int n = rand() %(2 ) - 1;

Closer. But, how many possible values are there between -1 and 1 ?
-1, 0, and -1.  Three values
>> -1, 0, and -1.  Three values

And how many possible values does this give ?

        int n = rand() %(2 ) - 1;
%(2) is 0 and 1, subracting two would give me -1.
>> %(2) is 0 and 1

Yes. So, subtracting 1 would give -1 and 0. Only two of the three values we needed.
so...........

e) -1 <= n <= 1
int n = rand() %(2 ) - 2;
>> e) -1 <= n <= 1
>> int n = rand() %(2 ) - 2;

Now you're back to guessing :)

Let's get back to this one which you had right :

>> c) 0 <= n <= 9
>> int n = rand() % (10);

Why did you choose 10 is the right operand for the modulo operator ?
because it gives you 0 and 9.
>> because it gives you 0 and 9.

because it gives you 0 to 9.

Or more precisely, because there are 10 values in the target range 0 <= n <= 9, namely 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

See a connection with e) ?
so there are three values in the range -1 <=n<=1;
so I would use %(3) based on that?
>> so I would use %(3) based on that?

Yes ... continue ... :)
so %(3) -1?  not sure..
>> so %(3) -1?  not sure..

If you're not sure, just do the calculation. What range would rand()%3 - 1 give you ?
0,1,2, wait a minute...

%(3)-3 would give 0,1,2 minus two = -1..
>> %(3)-3 would give 0,1,2 minus two = -1..

Why do you say -3 and "minus two" ? I thought you said -1 as in :

        rand()%3 - 1

What range would that give you ?
1 AND -1
I appreciate your time.  I have a firmer understanding., but not the firmest  I'm going to do some more research on it.  Thank you so much!!!!
>> 1 AND -1

Between -1 and 1 or :

        -1 <= n <= 1

which is what you wanted.


>> I have a firmer understanding., but not the firmest  I'm going to do some more research on it.

Give the other exercises a try ... You've still got d) and f) to do ;)