• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Question about Decimals in C++

cchen

Diamond Member
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??
 
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.
 
Back
Top