![]() |
| | #1 |
| Registered User Join Date: Sep 2005
Posts: 92
| Problem with random number generation 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 | |
| | #2 |
| Just Lurking 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |