Thread: Random number generator from minus to plus.HELP!

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    Unhappy 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. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by rogster001 View Post
    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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by grumpy View Post
    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. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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;
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    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. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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)
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Cat View Post
    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 subscribe to a feed

Similar Threads

  1. Only getting 0; random number generator
    By scatterbrain in forum C Programming
    Replies: 10
    Last Post: 10-11-2011, 06:24 AM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. random number generator
    By morrissey999 in forum C Programming
    Replies: 18
    Last Post: 05-01-2011, 04:53 PM
  4. A new random number generator!
    By Sebastiani in forum General Discussions
    Replies: 19
    Last Post: 07-30-2009, 03:27 PM
  5. Random number generator
    By Labmouse in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2007, 02:23 PM

Tags for this Thread