rand() and normalize

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    rand() and normalize

    Hello,

    I need to generates random number but between -0.5 and 0.5;
    Code:
    srand( time(NULL) );
    double number[5];
    for (int i=0; i < 5; i++) {
       number = rand() ;
    }
    What's the best way? Sincerly I haven't idea how to do; I could normalize them but to do they between -0.5 and 0.5 ? I thought to do numberNormalize d - 0.5; But I notice that in this way the final number are always negative....
    Any hints?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Does rand() return a double? Have you looked? If rand() does not return a double, then how do you get a decimal??

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      Originally posted by weaknessforcats
      Does rand() return a double? Have you looked? If rand() does not return a double, then how do you get a decimal??
      No, it doesn't; but I can assign it to a double and then normalize the values;
      Anyway; I know only rand() to generate numbers....

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        As w4cats suggests, you should really look at the documentation for rand(). The random number lies in the range 0 to RAND_MAX. So if you cast your random number to double, then divide it by RAND_MAX, you will have a random decimal number between 0 and 1. Then subtract 0.5.

        Incidentally, if you want an array, line 4 should be number[i] = rand();

        Comment

        Working...