C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-27-2007, 05:22 PM   #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;
}
HAssan is offline   Reply With Quote
Old 03-27-2007, 05:49 PM   #2
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,006
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.*
Dave_Sinkula is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22