Thread: random number between negative and positive number

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    17

    random number between negative and positive number

    Hi

    I've seen some other threads on this and the faq, but I can't seem to find the answer to this one.

    I want to generate a random number (non-integer) between a negative number (such as -1) and a positive number (such as 1). So I want a random number of type double between -1 and 1, how do I do this? Appreciate it if someone could guide me or point me to some useful info. Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like for example
    rand() % 3 - 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    anomaly is asking for a non-integer, one way would be:

    2.0f * ((float)rand() / (float)RAND_MAX) - 1.0f

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    17
    Hi

    Thanks for the example code. Could you also explain the syntax to me? I'm fairly new to c++, so I don't completely understand the syntax yet. Thanks

    More specifically, why do you place an 'f' after a number?
    Last edited by anomaly; 12-06-2003 at 08:02 AM.

  5. #5
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    the function 'rand()' returns a random integer between 0 and RAND_MAX, so to get a random number between 0 and 1 we can use rand() / RAND_MAX since rand() is always less than RAND_MAX, and you know, when using / with integers we get an integer, so we need to convert our integers to double or float first:
    (double)rand() / (double)RAND_MAX
    Now, this will give us a random between 0 and 1, to get a random number between -1 and 1:

    2 * (double)rand() / (double)RAND_MAX - 1

    hope you got it now.

    f in 2.0f is to tell the compiler that 2.0f is a float and not a double (this would be 2.0)

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    17
    Thanks I already worked out how the code worked before, but thanks for shedding light on the 'f' syntax.

  7. #7
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    np there are also u for unsigned, L for long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing from positive to negative numbers
    By vopo in forum C++ Programming
    Replies: 19
    Last Post: 09-10-2008, 02:21 PM
  2. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 07:12 AM
  3. OpenGL example cube is black, white, grey only
    By edwardtisdale in forum Windows Programming
    Replies: 7
    Last Post: 09-22-2007, 02:37 PM
  4. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM