How to generate non-negative random numbers(integer) using RNGCryptoServiceProvider C# -
i need generate non-negative random integers in code. example below generates integers;
using (rngcryptoserviceprovider rng = new rngcryptoserviceprovider()) { // buffer storage. byte[] data = new byte[4]; // ten iterations. (int = 0; < 10; i++) { // fill buffer. rng.getbytes(data); // convert int 32. int value = bitconverter.toint32(data, 0); console.writeline(value); } }
ref: http://www.dotnetperls.com/rngcryptoserviceprovider gives both positive , negative values. how generate non-negative random integers? earlier using random.next() giving me positive integers.
pseudocode:
repeat temp <- rng.nextinteger(); until temp >= 0; return temp;
Comments
Post a Comment