www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - generating random numbers

reply Andy Balba <pwplus7 gmail.com> writes:
generating random numbers using 
https://dlang.org/library/std/random/uniform01.html

I find the example given in this section totally incomprehensible
.. Can any help me answer two simple questions:
How to generate a random floating number in range [0,1) ?
How to set a seed value, prior to generating random values ?
Aug 09 2020
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 8/9/20 10:51 PM, Andy Balba wrote:
 generating random numbers using 
 https://dlang.org/library/std/random/uniform01.html
 
 I find the example given in this section totally incomprehensible
 ... Can any help me answer two simple questions:
 How to generate a random floating number in range [0,1) ?
 How to set a seed value, prior to generating random values ?
I think feqrel() is the confusing and unnecessary part there. The following is all you need: import std.stdio; import std.random; void main() { auto rnd = MinstdRand0(42); // <-- Seed foreach (i; 0 .. 10) { writeln(rnd.uniform01()); } } feqrel, defined at https://dlang.org/phobos/std_math.html#.feqrel is used to prove that the first two floating point values generated are equal to 0.000328707 and 0.524587. (Check out the return value of feqrel, which is used in assert statements there to prove that the numbers are "equal" to those.) Ali
Aug 09 2020
prev sibling next sibling parent James Blachly <james.blachly gmail.com> writes:
On 8/10/20 1:51 AM, Andy Balba wrote:
 generating random numbers using 
 https://dlang.org/library/std/random/uniform01.html
 
 I find the example given in this section totally incomprehensible
 .. Can any help me answer two simple questions:
 How to generate a random floating number in range [0,1) ?
 How to set a seed value, prior to generating random values ?
Tangential: I also find mir-random [0] a very nice library that is easy to use and has no deps on D runtime, so can be used in Das Better C mode. I think it is also a better random engine than phobos [0] https://code.dlang.org/packages/mir-random
Aug 10 2020
prev sibling parent reply bachmeier <no spam.net> writes:
On Monday, 10 August 2020 at 05:51:07 UTC, Andy Balba wrote:
 generating random numbers using 
 https://dlang.org/library/std/random/uniform01.html

 I find the example given in this section totally 
 incomprehensible
 .. Can any help me answer two simple questions:
 How to generate a random floating number in range [0,1) ?
 How to set a seed value, prior to generating random values ?
Strange example for sure. I'd recommend checking out the examples on the landing page for std.random: https://dlang.org/library/std/random.html
Aug 10 2020
parent reply bachmeier <no spam.net> writes:
On Monday, 10 August 2020 at 14:20:23 UTC, bachmeier wrote:
 On Monday, 10 August 2020 at 05:51:07 UTC, Andy Balba wrote:
 generating random numbers using 
 https://dlang.org/library/std/random/uniform01.html

 I find the example given in this section totally 
 incomprehensible
 .. Can any help me answer two simple questions:
 How to generate a random floating number in range [0,1) ?
 How to set a seed value, prior to generating random values ?
Strange example for sure. I'd recommend checking out the examples on the landing page for std.random: https://dlang.org/library/std/random.html
I created a PR with a hopefully clearer example: https://github.com/dlang/phobos/pull/7588
Aug 10 2020
parent reply Andy Balba <pwplus7 gmail.com> writes:
On Monday, 10 August 2020 at 15:13:51 UTC, bachmeier wrote:
 On Monday, 10 August 2020 at 14:20:23 UTC, bachmeier wrote:
 On Monday, 10 August 2020 at 05:51:07 UTC, Andy Balba wrote:
 generating random numbers using 
 https://dlang.org/library/std/random/uniform01.html

 I find the example given in this section totally 
 incomprehensible
 .. Can any help me answer two simple questions:
 How to generate a random floating number in range [0,1) ?
 How to set a seed value, prior to generating random values ?
Strange example for sure. I'd recommend checking out the examples on the landing page for std.random: https://dlang.org/library/std/random.html
I created a PR with a hopefully clearer example: https://github.com/dlang/phobos/pull/7588
Ahhh yes, yes .. this is the way to write Dlang example code : https://dlang.org/library/std/random.html
Aug 10 2020
parent Andy Balba <pwplus7 gmail.com> writes:
On Monday, 10 August 2020 at 15:43:04 UTC, Andy Balba wrote:
 On Monday, 10 August 2020 at 15:13:51 UTC, bachmeier wrote:
 On Monday, 10 August 2020 at 14:20:23 UTC, bachmeier wrote:
 On Monday, 10 August 2020 at 05:51:07 UTC, Andy Balba wrote:
 generating random numbers using 
 https://dlang.org/library/std/random/uniform01.html

 I find the example given in this section totally 
 incomprehensible
 .. Can any help me answer two simple questions:
 How to generate a random floating number in range [0,1) ?
 How to set a seed value, prior to generating random values ?
Strange example for sure. I'd recommend checking out the examples on the landing page for std.random: https://dlang.org/library/std/random.html
I created a PR with a hopefully clearer example: https://github.com/dlang/phobos/pull/7588
Ahhh yes, yes .. this is the way to write Dlang example code : https://dlang.org/library/std/random.html
... a very neat random byte generator is at https://github.com/LightBender/SecureD/tree/master/source/secured here's the essential code : import std.digest; import std.stdio; import std.exception; trusted ubyte[] random (uint bytes) { if (bytes == 0) { printf("number of bytes must be > zero"); return null; } ubyte[] buffer = new ubyte[bytes]; try { File urandom = File("/dev/urandom", "rb"); urandom.setvbuf (null, _IONBF); scope(exit) urandom.close(); try { buffer= urandom.rawRead(buffer); } catch(ErrnoException ex) { printf("Cant get next random bytes"); return null;} catch(Exception ex) { printf("Cant get next random bytes"); return null; } } catch(ErrnoException ex) { printf("Cant initialize system RNG"); return null; } catch(Exception ex) { printf ("Cant initialize system RNG"); return null; } return buffer; } void main() { ubyte[] rnd1 = random(32); writeln("32Bytes: ", toHexString!(LetterCase.lower)(rnd1)); ubyte[] rnd2 = random(128); writeln("128Bytes: ", toHexString!(LetterCase.lower)(rnd2)); ubyte[] rnd3 = random(512); writeln("512Bytes:"); writeln(toHexString!(LetterCase.lower)(rnd3)); ubyte[] rnd4 = random(2048); writeln("2048 Bytes:"); writeln(toHexString!(LetterCase.lower)(rnd4)); }
Aug 13 2020