Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to know how to generate a secure double defined on range [0.0 - 1.0] both inclusive. I generate two big ulong numbers with RNGCryptoServiceProvider class, make sure that the second number is greater or equals to the first number and then divide the first number by the second number and cast result to double. Ideally, numbers 0.0d and 1.0d should be included in this range, but they're never included. It must be something with mantissa with double values. Do you know how to fix it?

What I have tried:

Tried to generate two ULONG numbers, making sure that the second number was greater or equaled to the first number, divided second number by the first number and casted result to double
Posted
Updated 7-Jan-21 11:16am

1 solution

Any reason you're generating two numbers? x / MAX_ULONG will give you a random number between 0 and 1. And combining two random numbers that way doesn't increase the entropy - concatenating them would. But dividing them doesn't.
 
Share this answer
 
v2
Comments
NickResh777 7-Jan-21 17:16pm    
Ok, so when I generate only a single ulong number and divide it by ulong.MaxValue, will it be a truly secure generator with equal probability of all cases??
Chris Losinger 7-Jan-21 17:24pm    
Yes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900