none
How do I generate "True" Random numbers (float) using RNGCryptoService between 0 and 1

    Question

  • I want to generate random numbers using the RNGCryptoServiceProvider, but I want the numbers to be float or double between 0 and 1. How do I achieve this?

    Any help is appreciated.

    Sunday, October 24, 2010 6:01 PM

Answers


  • Hello again,

    I guess you could always do something like this:

          var b = new byte[12];
          var rngCrypto = new RNGCryptoServiceProvider();
          rngCrypto.GetBytes(b);
          var stringBuilder = new StringBuilder("0.");
          var numbers = b.Select(i => Convert.ToInt32((i * 100 / 255)/10)).ToArray();
          
          foreach (var number in numbers)
          {
            stringBuilder.Append(number);
          }
    
          var randomNumber = Convert.ToDouble(stringBuilder.ToString());
    

    It's not very clean, but it should work if you are keen on using RNGCryptoServiceProvider

     

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Godwinium Sunday, October 24, 2010 7:50 PM
    Sunday, October 24, 2010 7:00 PM

All replies

  • Hello,

     

    Why don't you just use Random for this?

          var randomNum = new Random().NextDouble();
    

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, October 24, 2010 6:17 PM
  • As I know  RNGCryptoServiceProvider returns bytes - not double. I am not sure if it can be used for a random double numbers!
    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    Sunday, October 24, 2010 6:24 PM
  • I know how to do it with Random(), wanted to do a simulation that requires a better Random number generator, was wondering if I could do this with RNGCryptoService.
    Sunday, October 24, 2010 6:26 PM
  • Actually you can use BitConverter to convert the bytes to other data types. e.g

    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

    byte[] rand = new byte[4];

    rng.GetBytes(rand);

    rand2 = BitConverter.ToSingle(rand, 0);

    I just want to know how to restrict the numbers to between 0 and 1

     

    Sunday, October 24, 2010 6:32 PM

  • Hello again,

    I guess you could always do something like this:

          var b = new byte[12];
          var rngCrypto = new RNGCryptoServiceProvider();
          rngCrypto.GetBytes(b);
          var stringBuilder = new StringBuilder("0.");
          var numbers = b.Select(i => Convert.ToInt32((i * 100 / 255)/10)).ToArray();
          
          foreach (var number in numbers)
          {
            stringBuilder.Append(number);
          }
    
          var randomNumber = Convert.ToDouble(stringBuilder.ToString());
    

    It's not very clean, but it should work if you are keen on using RNGCryptoServiceProvider

     

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Godwinium Sunday, October 24, 2010 7:50 PM
    Sunday, October 24, 2010 7:00 PM
  • That does the trick. Thanks Varga.
    Sunday, October 24, 2010 7:50 PM