Thread: Random array generator for floats

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Random array generator for floats

    Hi. I have come up with this for a random array generator for integers, but i can't seem to get to get it to work with floats. I changed some of the variables to floats but it didn't work. any ideas?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
      int i;
      unsigned int iseed = (unsigned int)time(NULL);
      srand (iseed);
    
      for (i=0; i<5; i++)
      {
        printf ("rand[%d]= %u\n",i, rand ());
      }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Take it in three parts:

    1) generate and assign a random integer to the digits on the left side of the decimal point.

    2) generate and assign the digits on the right hand side of the decimal of the float

    3) generate and assign a positive or negative sign to the number, if needed.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Or if you look up the range of random integers that rand() generates, you may just want to divide the result by some constant.

    For example: rand() / 1000.0

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you want a particular range of float/double values, say from 30 to 90, you could do something like:
    Code:
    double rand_value;
    
    rand_value = (double)rand() / RAND_MAX;  // generate a random number between 0 and 1
    rand_value *= (high - low);  // scale it to fit between high and low, range now from 0 to high-low
    rand_value += low;  // offset it by low, range now from low to high
    
    /* same thing in one statement */
    rand_value = ((double)rand() / RAND_MAX) * (high - low) + low;

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Hi,

    Thanks for the reply's have I have divided the rand() by a constant and it seems to work well. My question now is how can I access the array. would the array be rand[]?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would access the array as usual. One problem now is that your code does not actually assign the numbers generated to the elements of an array.
    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

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    That's what i was thinking. It prints out like so:

    rand[0] = floating point number 1
    rand[1] = floating point number 2
    rand[2] = floating point number 3
    rand[3] = floating point number 4

    I have to work out some way of assigning arrayname[0] = floating point number 1

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tomelk31 View Post
    That's what i was thinking. It prints out like so:

    rand[0] = floating point number 1
    rand[1] = floating point number 2
    rand[2] = floating point number 3
    rand[3] = floating point number 4

    I have to work out some way of assigning arrayname[0] = floating point number 1
    Giving an array (or other variable) the same name as a function can be problematic. You should pick a different name for the array... randnum[] or such...
    Last edited by CommonTater; 02-19-2011 at 07:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  3. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM