The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## Quick question

This is a discussion on Quick question within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; What would be the C equivalent to math.random(int, int) in lua, where the first int would be the first number, ...

 Tweet
1. ## Quick question

What would be the C equivalent to math.random(int, int) in lua, where the first int would be the first number, and the second int would be the second number, and it randomly gets a number between those two ints. Im just looking for maybe a simple function where I can insert two ints and it randomly gets a number between the two. Thanks

Code:
```// between 0 and i including 0 and excluding i
int randomNum(int i) {
int j;
srand( (unsigned int)time( NULL ) );
j = i * rand() / (RAND_MAX + 1.00);
return j;
}```
I came up with that, but I think its returning the initial 'int i' that gets put in, or its jsut crashing, im not sure.

2. Not sure on the above code, but I found this code made by cyberbill and have been using it since -

Above the main function,
Code:
```find a random number between two numbers */
int Random(int min, int max)
{
int Diff = max - min;
return (rand() % Diff)+min;
}```
In the main function (only call it once) -
Code:
```/* Seed rand() with the clocktime to give the appearance of randomness*/
srand(sceKernelLibcTime(NULL));```
Best of luck

3. Thanks! just what I was looking for

4. There's a simpler way of doing it.
First you have to type srand(time(NULL)); only once to set up the randomizing (i suggest in the main() part of the code).
Then, you type min+rand()%max (where min is the lowest number and max is the highest).
Simple as that, and much easier to remember.

5. Originally Posted by alatnet
There's a simpler way of doing it.
First you have to type srand(time(NULL)); only once to set up the randomizing (i suggest in the main() part of the code).
Then, you type min+rand()%max (where min is the lowest number and max is the highest).
Simple as that, and much easier to remember.
That doesnt do the same thing. The max number for that is min+max.

6. It isnt as clean to me, so i wont use it. I take it with how picky arrays are about using variables in them, how could you have an array randomly pick a number in the array, and do that action? For example, have a random image out of 3 (cant be 2, or else it would be to weired and pointless), be picked and blitted for 10 seconds, then next etc. Someting like:

Code:
```Image* arrayofimages[3];
//load images and things into buffers and stuff

Random(arrayofimages[0], arrayofimages[1])```
Wait, the protoype says inetger, not Image*, so i geuss some how get a variable into the array? Bt how? maybe a pointer? like: int blah* blah = x? or what? Smerity, sorry for bothering you if you answer :icon_sad: ... I pester you too much, but i cnat help it, wnat to learn

7. SG57 - I think you just posted in the wrong thread...

8. Originally Posted by xigency
That doesnt do the same thing. The max number for that is min+max.
Oh yes it does! Here's an example:

Code:
`1+rand()%6`
This code pick's a random number between 1 and 6.

Code:
`rand()%6`
This code pick's a random number between 0 and 6.

Do you get it now?

9. Alatnet, Smerty and Xigency were right. The order of operations for C are fairly standard and the modulus operator (%) is grouped with the multiplication and division group. So taking that into account:

answer = min + rand() % max ( Not Correct )

Because there exists a rand() value that would put the answer out of the {min, max} range. In this case, every time it fails it will fail by exceeding the max value. For example:

min = 2;
max = 6;
rand() = 35;
answer = min + rand() % max;

The original formula of:
answer = (rand() % (max - min)) + min
is correct assuming that max is always greater than min.

Anyways, just thought I'd add my 2 cents.

10. Originally Posted by codemonkey
Alatnet, Smerty and Xigency were right. The order of operations for C are fairly standard and the modulus operator (%) is grouped with the multiplication and division group. So taking that into account:

answer = min + rand() % max ( Not Correct )

Because there exists a rand() value that would put the answer out of the {min, max} range. In this case, every time it fails it will fail by exceeding the max value. For example:

min = 2;
max = 6;
rand() = 35;
answer = min + rand() % max;

The original formula of:
answer = (rand() % (max - min)) + min
is correct assuming that max is always greater than min.

Anyways, just thought I'd add my 2 cents.
using the following data:
min = 2;
max = 6;
rand() = 35;

and using the following code:
answer = min + rand() % max;

It equals 2 because:
1. min + rand() = 37 (2+35=37)
2. 37 / max = 6.166666667 (37 / 6 = 6.166666667)
but instead of the "/" operand, it uses the "%". Because of this the answer doesn't equals 6, it equals 2 because the "%" rounds the tenths place number resulting in 2 (e.g. 6.166666667 becomes 0.2).

This proves that "min + rand() % max" is simpler and efficient.

11. Alatnet,
I said the order of operations is modulus (%) before addition / subtraction. So:

min = 2;
max = 6;
rand () = 35;
answer = 2 + rand() % 6
= 2 + (35 % 6)
= 2 + (5)
= 7

Also, I think you're misunderstanding how the modulus operator works. Think of the modulus operator as the remainder part of the division problems you did in the elementary school.

With your formula, answer = min + rand() % max, you will be wrong (min - 1) times for every max calculations. Obviously that's only if you do a great many calculations that you'll see that trend. But it's there.

Here's an online modulus calculator. There's probably a few more, but this is the first one that I found.

-CodeMonkey

12. Originally Posted by codemonkey
Also, I think you're misunderstanding how the modulus operator works. Think of the modulus operator as the remainder part of the division problems you did in the elementary school.
I do know how the modulus operator works, i just forgot the remainder word.

Originally Posted by codemonkey
With your formula, answer = min + rand() % max, you will be wrong (min - 1) times for every max calculations. Obviously that's only if you do a great many calculations that you'll see that trend. But it's there.
Originally Posted by Book: A First Book of C++ From Here to There by Gary J. Bronson. Page: 51

TABLE 2.7 Operator Precedence and Associativity
Operator | Associativity
| unary - | right to left |
+----------+--------------+
| * / % | left to right |
+----------+--------------+
| + - | left to right |
+----------+--------------+

13. Originally Posted by alatnet
I do know how the modulus operator works, i just forgot the remainder word.
You dont understand. C++ uses order of operations. Like PEMDAS. Modulus comes before addition/subtraction.

And that "left to right" is the associativity of operators.

It also shows that % comes before +/- if you read it correctly.

TABLE 2.7 Operator Precedence and Associativity
Operator | Associativity
1.| unary - | right to left |
+----------+--------------+
2.| * / % | left to right |
+----------+--------------+
3.| + - | left to right |
+----------+--------------+

It shows how * / % have precedence over + -.

14. i've been programming in C++ for a few months now (programming class in my high school) and i know for a fact that "min+rand%max" produces a random number between min and max! So don't fracking contradict me bout how it's wrong, cus i've beem using it in programs of mine.

To top it all off, im gona show you the pages that have that random code in the book! (You need Photoshop to view these pictures, cus Photoshop can zoom in on a picture)

Page 1:
http://fire.prohosting.com/alatnet/Pics/Rand1.pdf

Page 2:
http://fire.prohosting.com/alatnet/Pics/Rand2.pdf

Page 3:
http://fire.prohosting.com/alatnet/Pics/Rand3.pdf

P.S.
Sry if i sounded harsh.

15. Alatnet,
I've already proven mathmatically that you're wrong. If you don't believe the math, then plug this into your compiler and see what happens.

Code:
```int min = 2;
int max = 6;
int rand = 35;
int answer = min + rand % max;
Then when you see that the answer in that case is 7 you'll know that there is an error in your math. You may have been programming C++ for a few months now in middle school but I've been programming C++ for a little over 10 years.

Not that any of that really matters. Just plug the code in if you don't believe me. I did, just to double check that the compilers were complient with the language definition. I ran it in gcc, Visual Studio, and Borland. All 3 gave me 7. If you're right and I'm wrong, prove it. Don't just say, "I'm right because I know". I have given you formulas and code showing you that I'm right and all you've given us is your word that you saw it in a book somewhere. Anecdotal at best, it could be that you remember wrong, or that your school book isn't worth the paper it's printed on. Either way, evaluate the proof that's been given here and bring your own proof for me to evaluate.

16. yet you didn't have it randomize it's self!
i've used this code:
Code:
```srand(time(NULL));
int min = 1;
int max = 6;
int randa = rand();
int answer = min + randa % max;
cout << randa << '\n';```
and i never got any 7's. I think the min always has to be 1.

17. Alatnet,
I told you that you would be wrong (min - 1) times per max runs of the formula. Obviously if min = 1, then you don't have a problem. Set the min up to 2 and see what happens. Or better yet, to exaggerate the problem, set min = 10 and max = 12 and run it a few times. Here's the breakdown:

min = 10
max = 12

1 : 11
2 : 12
3 : 13
4 : 14
5 : 15
6 : 16
7 : 17
8 : 18
9 : 19
10 : 20
11 : 21
12 : 10

So for max runs, your formula is wrong min-1 times. Raise the min up, and run it a few times. I already conceded in post 10 that you would be wrong min - 1 times per max. Is that why you picked a min of 1?

-CodeMonkey

18. I did notice that i was getting an answer of 7 when i use 2 for min, i guess you were right (sry).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 03:59 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY