www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile-Time RNG

reply CTRNG <ctrng example.com> writes:
I managed to create a compile-time random number generator.

Proof of concept, with some explanatory comments: 
http://dpaste.dzfl.pl/668646ce6d71

Just thought this might be of interest to some of you here.
Jan 20 2016
next sibling parent reply tsbockman <thomas.bockman gmail.com> writes:
On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 I managed to create a compile-time random number generator.

 Proof of concept, with some explanatory comments: 
 http://dpaste.dzfl.pl/668646ce6d71

 Just thought this might be of interest to some of you here.
That's clever (and devious).
Jan 20 2016
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 01/21/2016 01:32 AM, tsbockman wrote:
 On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 I managed to create a compile-time random number generator.

 Proof of concept, with some explanatory comments:
 http://dpaste.dzfl.pl/668646ce6d71

 Just thought this might be of interest to some of you here.
That's clever (and devious).
It only works because compile-time introspection is ill-defined though. I don't expect it to keep working indefinitely.
Jan 20 2016
next sibling parent reply tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 21 January 2016 at 01:49:27 UTC, Timon Gehr wrote:
 It only works because compile-time introspection is ill-defined 
 though. I don't expect it to keep working indefinitely.
That aspect can easily be replaced by __LINE__ and __FILE__.
Jan 20 2016
parent reply Matthias Bentrup <matthias.bentrup googlemail.com> writes:
On Thursday, 21 January 2016 at 07:43:13 UTC, tsbockman wrote:
 On Thursday, 21 January 2016 at 01:49:27 UTC, Timon Gehr wrote:
 It only works because compile-time introspection is 
 ill-defined though. I don't expect it to keep working 
 indefinitely.
That aspect can easily be replaced by __LINE__ and __FILE__.
A supported way to generate new names at compile time would be nice.
Jan 21 2016
parent tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 21 January 2016 at 09:29:38 UTC, Matthias Bentrup 
wrote:
 That aspect can easily be replaced by __LINE__ and __FILE__.
A supported way to generate new names at compile time would be nice.
It would, but in this particular case __LINE__ and __FILE__ actually provide a more natural way of solving the problem. The idea of generating new symbols to increment the counter is clever, but it's also an awkward hack. __LINE__ already does pretty much what he needed by itself.
Jan 21 2016
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Thursday, 21 January 2016 at 01:49:27 UTC, Timon Gehr wrote:
 On 01/21/2016 01:32 AM, tsbockman wrote:
 On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 Just thought this might be of interest to some of you here.
That's clever (and devious).
It only works because compile-time introspection is ill-defined though. I don't expect it to keep working indefinitely.
Hmm it does seem having an RNG during compile-time would be useful, although I'm trying to come up with a use-case for when it would actually work, or how you'd use it.
Jan 21 2016
prev sibling next sibling parent Brian Schott <briancschott gmail.com> writes:
On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 I managed to create a compile-time random number generator.

 Proof of concept, with some explanatory comments: 
 http://dpaste.dzfl.pl/668646ce6d71

 Just thought this might be of interest to some of you here.
That's nearly as fun as using the bsr and bsf functions in core.bitop to implement a pure random number generator. https://issues.dlang.org/show_bug.cgi?id=14380
Jan 20 2016
prev sibling parent reply Cym13 <cpicard openmailbox.org> writes:
On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 I managed to create a compile-time random number generator.

 Proof of concept, with some explanatory comments: 
 http://dpaste.dzfl.pl/668646ce6d71

 Just thought this might be of interest to some of you here.
Sorry for necroposting but does anyone have that POC? dpaste.dzfl.pl seems down. I'm currently facing a case where I would very much like to have random data at compile-time and I can't find a way to get them. Also the core.bitopt.bsf bug mentionned later in the thread was fixed so no luck there (and the bug report is still open, I think it should be closed).
Mar 05 2019
next sibling parent Dennis <dkorpel gmail.com> writes:
On Tuesday, 5 March 2019 at 21:22:55 UTC, Cym13 wrote:
 Sorry for necroposting but does anyone have that POC?
It's on the D idioms list https://p0nce.github.io/d-idioms/#Compile-time-RNG
Mar 05 2019
prev sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 5 March 2019 at 21:22:55 UTC, Cym13 wrote:
 On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 I managed to create a compile-time random number generator.

 Proof of concept, with some explanatory comments: 
 http://dpaste.dzfl.pl/668646ce6d71

 Just thought this might be of interest to some of you here.
Sorry for necroposting but does anyone have that POC? dpaste.dzfl.pl seems down. I'm currently facing a case where I would very much like to have random data at compile-time and I can't find a way to get them. Also the core.bitopt.bsf bug mentionned later in the thread was fixed so no luck there (and the bug report is still open, I think it should be closed).
it's easy. you use the time_stamp at compilation which is expressed as a certain lexer variable. you parse that at ctfe and use it as the seed for another random number gen.
Mar 05 2019
next sibling parent Cym13 <cpicard openmailbox.org> writes:
On Tuesday, 5 March 2019 at 22:57:31 UTC, Stefan Koch wrote:
 On Tuesday, 5 March 2019 at 21:22:55 UTC, Cym13 wrote:
 On Wednesday, 20 January 2016 at 23:21:04 UTC, CTRNG wrote:
 [...]
Sorry for necroposting but does anyone have that POC? dpaste.dzfl.pl seems down. I'm currently facing a case where I would very much like to have random data at compile-time and I can't find a way to get them. Also the core.bitopt.bsf bug mentionned later in the thread was fixed so no luck there (and the bug report is still open, I think it should be closed).
it's easy. you use the time_stamp at compilation which is expressed as a certain lexer variable. you parse that at ctfe and use it as the seed for another random number gen.
Thank you both!
Mar 06 2019
prev sibling parent reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On Tuesday, 5 March 2019 at 22:57:31 UTC, Stefan Koch wrote:
 it's easy. you use the time_stamp at compilation which is 
 expressed as a certain lexer variable. you parse that at ctfe 
 and use it as the seed for another random number gen.
Depending on the use case, that might be problematic. Consider two different compilation units issued in the same second (so same timestamp). Both use the CTFE RNG, and because their seed is the same, so is the pseudo-random sequence.
Mar 07 2019
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Thursday, 7 March 2019 at 09:53:58 UTC, Joseph Rushton 
Wakeling wrote:
 On Tuesday, 5 March 2019 at 22:57:31 UTC, Stefan Koch wrote:
 it's easy. you use the time_stamp at compilation which is 
 expressed as a certain lexer variable. you parse that at ctfe 
 and use it as the seed for another random number gen.
Depending on the use case, that might be problematic. Consider two different compilation units issued in the same second (so same timestamp). Both use the CTFE RNG, and because their seed is the same, so is the pseudo-random sequence.
Using that in any way that's not a toy example is ill'advised. I'd say :)
Mar 07 2019