digitalmars.D.learn - Random Numbers
- Ralph Main (29/29) Aug 04 2012 Gotchas!
- Philippe Sigaud (12/22) Aug 04 2012 (snip)
- Ralph Main (12/24) Aug 04 2012 DMD32 D Compiler v2.059 on Linux (Fedora 17)
- Philippe Sigaud (3/7) Aug 04 2012 In your previous code, did you put it in the module scope or in a
- Ralph Main (13/26) Aug 04 2012 It was in the scope of a public function in a class. I was
- bearophile (36/66) Aug 04 2012 This code works, but it's a bad idea to create a new generator
- Ralph Main (8/35) Aug 04 2012 get_random is inside of a class. Because of scope problems, I
- Ralph Main (3/40) Aug 04 2012 I did take out the new keyword. So I guess the garbage collector
Gotchas! The module std.random documentation doesn't work as per the examples. The example shows getting a random number by the following code does not work: <code> // Generate a uniformly-distributed integer in the range [0, 14] auto i = uniform(0, 15); // Generate a uniformly-distributed real in the range [0, 100) // using a specific random generator Random gen; auto r = uniform(0.0L, 100.0L, gen); </code> <code> // Gets a random number int get_random() { auto rng = new Random(unpredictableSeed); auto rn = uniform(0, m_files.length, rng); return rn; } </code> The new keyword was not in the example, and the original example code would not work. When looking at the source code of the std libraries, a struct can contain a constructor, so therefore it is similar to a class; and on a whim I tried the new keyword. So I thought I would pass this information along. I looked at other posts in the forum, but didn't see anyone using the new keyword. Is this a bug, or a change to the D language implementation?
Aug 04 2012
On Sat, Aug 4, 2012 at 6:40 PM, Ralph Main <rmain1972 live.com> wrote: Hello Ralph,The module std.random documentation doesn't work as per the examples. The example shows getting a random number by the following code does not work:(snip) Both code samples you gave work perfectly for me (DMD 2.060, Linux). Which version are you using?The new keyword was not in the example, and the original example code would not work. When looking at the source code of the std libraries, a struct can contain a constructor, so therefore it is similar to a class; and on a whim I tried the new keyword. So I thought I would pass this information along. I looked at other posts in the forum, but didn't see anyone using the new keyword. Is this a bug, or a change to the D language implementation?The 'new' keyword has been in D from the beginning, but it's used to create classes (which are reference types in D) or values on the heap. Most of the time, you create a struct like this: MyStruct s = MyStruct(arguments); For a class: MyClass c = new MyClass(arguments); See: http://dlang.org/expression.html#NewExpression
Aug 04 2012
On Saturday, 4 August 2012 at 16:59:09 UTC, Philippe Sigaud wrote:On Sat, Aug 4, 2012 at 6:40 PM, Ralph Main <rmain1972 live.com> wrote: Hello Ralph,DMD32 D Compiler v2.059 on Linux (Fedora 17) All I know, is that it wasn't working, and I finally got it to work when I used the new keyword. I tried the code in just main() like bearophile's post, and it worked just fine. I also removed the new keyword from my program and it compiled. The error message that I got yesterday with the example code returned an error message that had something do with void and 0 arguments. <shrug> Thanks to both of you for the information.The module std.random documentation doesn't work as per the examples. The example shows getting a random number by the following code does not work:(snip) Both code samples you gave work perfectly for me (DMD 2.060, Linux). Which version are you using?
Aug 04 2012
On Sat, Aug 4, 2012 at 7:27 PM, Ralph Main <rmain1972 live.com> wrote:I tried the code in just main() like bearophile's post, and it worked just fine. I also removed the new keyword from my program and it compiled. The error message that I got yesterday with the example code returned an error message that had something do with void and 0 arguments.In your previous code, did you put it in the module scope or in a function? (main() is a function)
Aug 04 2012
On Saturday, 4 August 2012 at 20:22:23 UTC, Philippe Sigaud wrote:On Sat, Aug 4, 2012 at 7:27 PM, Ralph Main <rmain1972 live.com> wrote:It was in the scope of a public function in a class. I was experimenting with the D language. Trying it out. I like it, but there are some things that I don't like (but that is true of any programming language.) class A { private: //Code public: int get_random() { //Code } }I tried the code in just main() like bearophile's post, and it worked just fine. I also removed the new keyword from my program and it compiled. The error message that I got yesterday with the example code returned an error message that had something do with void and 0 arguments.In your previous code, did you put it in the module scope or in a function? (main() is a function)
Aug 04 2012
Ralph Main:The module std.random documentation doesn't work as per the examples. The example shows getting a random number by the following code does not work: <code> // Generate a uniformly-distributed integer in the range [0, 14] auto i = uniform(0, 15); // Generate a uniformly-distributed real in the range [0, 100) // using a specific random generator Random gen; auto r = uniform(0.0L, 100.0L, gen); </code> <code> // Gets a random number int get_random() { auto rng = new Random(unpredictableSeed); auto rn = uniform(0, m_files.length, rng); return rn; } </code>This code works, but it's a bad idea to create a new generator inside getRandom(): import std.stdio, std.random; // Gets a random number, badly int getRandom(int m) { auto rng = new Random(unpredictableSeed); return uniform(0, m, rng); } void main() { // Generate a uniformly-distributed integer in the range [0, 14] auto i = uniform(0, 15); // Generate a uniformly-distributed real in the range [0, 100) // using a specific random generator Random gen; auto r = uniform(0.0L, 100.0L, gen); writeln(r); writeln(getRandom(10)); }The new keyword was not in the example, and the original example code would not work. When looking at the source code of the std libraries, a struct can contain a constructor, so therefore it is similar to a class; and on a whim I tried the new keyword. So I thought I would pass this information along. I looked at other posts in the forum, but didn't see anyone using the new keyword. Is this a bug, or a change to the D language implementation?In D you instantiate a class with new, it generally gets allocated on the heap, and what you obtain is a class reference, that is a kind of pointer. Structs can be allocated with new, usually on the heap, and you get a pointer to a struct. Or they can be created locally without "new", often on the stack or inside another struct/class instance, and what you obtain is a struct value. std.random.Random is a struct. std.random.uniform() as third optional value seems to accept both a struct pointer and a struct (that it takes by reference, so using a pointer adds another indirection level, and this is not good. I don't know if the D compiler is able to remove this extra indirection level). Are my answers enough? Bye, bearophile
Aug 04 2012
On Saturday, 4 August 2012 at 17:02:19 UTC, bearophile wrote:Ralph Main:get_random is inside of a class. Because of scope problems, I don't think I can put it anywhere else. It would be better if you could create rng as int rng; rng = Random(unpredictableSeed); then you would be able to put it elsewhere. RalphThe module std.random documentation doesn't work as per the examples. The example shows getting a random number by the following code does not work: <code> // Generate a uniformly-distributed integer in the range [0, 14] auto i = uniform(0, 15); // Generate a uniformly-distributed real in the range [0, 100) // using a specific random generator Random gen; auto r = uniform(0.0L, 100.0L, gen); </code> <code> // Gets a random number int get_random() { auto rng = Random(unpredictableSeed); auto rn = uniform(0, m_files.length, rng); return rn; } </code>This code works, but it's a bad idea to create a new generator inside getRandom():
Aug 04 2012
On Saturday, 4 August 2012 at 17:45:19 UTC, Ralph Main wrote:On Saturday, 4 August 2012 at 17:02:19 UTC, bearophile wrote:I did take out the new keyword. So I guess the garbage collector will be happier. ;)Ralph Main:get_random is inside of a class. Because of scope problems, I don't think I can put it anywhere else. It would be better if you could create rng as int rng; rng = Random(unpredictableSeed); then you would be able to put it elsewhere. RalphThe module std.random documentation doesn't work as per the examples. The example shows getting a random number by the following code does not work: <code> // Generate a uniformly-distributed integer in the range [0, 14] auto i = uniform(0, 15); // Generate a uniformly-distributed real in the range [0, 100) // using a specific random generator Random gen; auto r = uniform(0.0L, 100.0L, gen); </code> <code> // Gets a random number int get_random() { auto rng = Random(unpredictableSeed); auto rn = uniform(0, m_files.length, rng); return rn; } </code>This code works, but it's a bad idea to create a new generator inside getRandom():
Aug 04 2012