Thread: Problem with random number generation

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Problem with random number generation

    Hello

    I wrote this program to generate 256 random numbers. The numbers change each time the code is excuted. But I can't seem to limit the numbers from 1 to -1. Antother thing I can't seem to do is that I want the numbers to go up to 2 decimal placec. i.e. 1.11. I will be highly obliged if someone can point me inthe right direction.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    
        float i,a;
    
        /* Set evil seed (initial seed) */
        srand( (unsigned)time( NULL ) );
    
        for (i = 0; i < 256; i++) 
        {
            a=rand()/10000;	
            printf("%f\n",a);
        }
       
        return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    a = rand() / (RAND_MAX / 2.0F) - 1;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number issue
    By swgh in forum C++ Programming
    Replies: 15
    Last Post: 11-14-2008, 10:12 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Random number generation
    By scrub05 in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2005, 08:20 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number Generation
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 11-21-2001, 04:29 AM