Float giving only integer part of the value

I wrote the following program to generate 100 random values between 0 and 1 using linear congruential generator.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int M = 100;
int a = rand() % 5 + 1;
cout<<a<<"\n";
int c = rand() % 3 + 1;
cout<<c<<"\n";
int X = 1;
int i;
float U[100];
for(i=0; i<100; i++)
{
U[i] = X/M;
cout<<U[i]<<" ";
cout<<X;
X = (a*X + c) % M;
}
int k;
cin>>k;
return 0;
}

However all U[i] values are shown as 0 in the output. I am using bloodshed dev c++ latest version (5 beta) for compiling and running the code. Can someone please tell me why I cannot get the correct value of U?
Both X and M are integers, so the equation X/M yields an integer value, which only then is assigned to your float u[i].

Make sure to cast first:
 
  U[i] = float(X) / M;

Hope this helps.

PS. Please use [code] tags.
Thanks a lot for the reply. Will try it out and let know what happens.
Topic archived. No new replies allowed.