# Random number generator from minus to plus.HELP!

This is a discussion on Random number generator from minus to plus.HELP! within the C++ Programming forums, part of the General Programming Boards category; Hi there, I have to write a c++ program which says if arrow hit the target or don't. Place where ...

1. ## Random number generator from minus to plus.HELP!

Hi there,

I have to write a c++ program which says if arrow hit the target or don't. Place where arrow hit is determined with random number generator (x and y) coards. Place where the arrow hit is (-12; 12).

1.Target is 10 units. Count how much of 10 shots hitting the target.

First of all, i don't know how to make random number generator from -12 to 12.:/ That's why i am stuck.

I will be grateful for any help.

2. do you have any code so far? can you generate random numbers say between 1 and 10? we dont know what your understanding and work you have done is until you post your code. Do you have a particular problem with the range being defined as -n to n ? you could perhaps just use a range of 24 as a quick fix and treat 12 as 0

3. Originally Posted by rogster001
do you have any code so far? can you generate random numbers say between 1 and 10? we dont know what your understanding and work you have done is until you post your code. Do you have a particular problem with the range being defined as -n to n ? you could perhaps just use a range of 24 as a quick fix and treat 12 as 0
I dont have any code so far cuz i am stuck with these random numbers. Here is what u asked:
Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
srand(time(0));

cout << 1+(rand()%10) << endl;

return 0;
}```

4. Look in <cstdlib> for a couple of functions named srand() and rand().

Once seeded with srand(), rand() produces a pseudo-random value between 0 and RAND_MAX (where RAND_MAX is typically a somewhat largish integer).

To convert a sampled value, let's call it x, that is in range [0, RAND_MAX] to a value in the range [min, max], compute x%(max - min + 1) + min.

The distribution of computed you get by this approach doesn't have particularly wonderful statistical properties but this is enough to get you started.

5. Originally Posted by grumpy
Look in <cstdlib> for a couple of functions named srand() and rand().

Once seeded with srand(), rand() produces a pseudo-random value between 0 and RAND_MAX (where RAND_MAX is typically a somewhat largish integer).

To convert a sampled value, let's call it x, that is in range [0, RAND_MAX] to a value in the range [min, max], compute x%(max - min + 1) + min.

The distribution of computed you get by this approach doesn't have particularly wonderful statistical properties but this is enough to get you started.
Code:
`cout << x%(rand()12 + -12 + 1) -12;`
somehow i wrote code for -12, i dont get it:/

6. Code:
```x = rand();
x = x% (12-(-12)+1) + (-12);```
Remember to test the result to see if it is suitable for your purposes (eg it is "random" enough). If not, find another approach.

7. Maybe this will help you understand how the formula works (sometimes it's easy to use a formula without comprehending how it works):

rand() gives a random number over a large range - too large for your purposes. You can use the modulus operator to spread those numbers over a smaller range.

rand() % n will give you n possible numbers (from 0 to n-1). You seem to get this point already.

In your case, you want a total of 25 possibilities (12 negative numbers, 12 positive numbers, and 0). So you start with rand() %25. This gives you a random number from 0 ... 24.

But you need to shift that range down by 12, because you want the lowest number to be -12, rather than 0, and the highest to be 12, rather than 24. That's easy - just subtract 12.

Final result: rand() % 25 - 12;

8. Thanks it's clear now. But i am not sure how to sum the coards i know the formula of Hypotenuse, but nothing more ;/
Code:
```#include <iostream>#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()
{
int l;
srand(time(0));
int x = rand();
x = x% (12-(-12)+1) + (-12);
int y = rand();
y = y% (12-(-12)+1) + (-12);
cout << x << endl;
cout << y << endl;
x^2+y^2=l^2

return 0;
}```

9. This:

x^2+y^2=l^2

is not going to work. Assignment in a programming language is right - to - left - the right hand of the equals sign is evaluated, and the result assigned to the variable on the left.

You need to solve the equation for l, so that you could write:

l = (your expression here)

10. Originally Posted by Cat
This:

x^2+y^2=l^2

is not going to work. Assignment in a programming language is right - to - left - the right hand of the equals sign is evaluated, and the result assigned to the variable on the left.

You need to solve the equation for l, so that you could write:

l = (your expression here)
Thanks You all! Can close this thread now!

Popular pages Recent additions