Question about Decimals in C++

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
So I'm programming a simple Linear Congruential Generator (basically a random number generator)


So that part works. Now I want to transform the numbers I get to ~ U(0,1) (uniform distribution between 0 and 1. The way to do this is to take the values you get from the generator and divide by m )

So after running both functions (with m = 1572), I check the arrays. The array storing the randomly generated numbers is fine.... but the array storing the transformed numbers are all 0.0. Am I doing something wrong??

1
22
211
340
1501
946
667
1300

Those are the first few numbers generated.....

so the numbers in the U array should be

.000636
.01399
.1342
.216
.9548
.6017
etc

Anyone see the problem??
 

klah

Diamond Member
Aug 13, 2002
7,070
1
0
X[i] / m

is an operation using two integers, so the result is an integer. c++ truncates the result in integer maths so .9548=0, .6017=0, etc, which is then converted to a float and stored in the array.