Thread: drand48(): generating pseudo random numbers in a range

  1. #1
    Registered User
    Join Date
    Aug 2007

    drand48(): generating pseudo random numbers in a range


    I know you are going to tell me to search in the faq. But my problem is not there.

    The thing is this: I diveded the space between -2 and 2, both in x and in y axes, in little cells of 0.04x0.04. I have to generate random numbers in each cell, in that range, but I have not found the way to manipulate drand48() function to do that.

    ¿Some idea?

    Thanks a lot!"!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    A search of the Web reveals that:
    The drand48() and erand48() functions return non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0 , 1.0].

    I note that 0.5 * 4 = 2 and -0.5 * 4 = -2. As such, you probably can get away with (drand48() - 0.5) * 4.0 to satisfy your range.

    Oh wait, "in little cells of 0.04x0.04". Basically, you have 10000 cells? In that case it seems easier to use something like rand() to map out 10000 cells instead of risking floating point inaccuracy when rounding.
    Last edited by laserlight; 08-31-2007 at 11:23 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2007
    I found the way!!!

    I need numbers between 0 and 0.04; next, between 0.04 and 0.08, and so on.

    The algorithm should look like this:

    r=drand48()/25+i, where i is 0, 0.04, 0.08, and so on.

    Thanks anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  3. Need Help Generating Random Numbers In C++
    By slickwilly440 in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2005, 01:38 PM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. generating random numbers within a range
    By tucky in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 12:59 PM