digitalmars.D.learn - get random char from array
- SaltySugar (6/6) Feb 02 2013 My code:
- bearophile (13/14) Feb 02 2013 In Phobos there isn't something like random.choice() of Python.
- SaltySugar (2/16) Feb 02 2013 Thank you!
My code: import std.stdio; import std.random; void main() { }
Feb 02 2013
SaltySugar:My code:In Phobos there isn't something like random.choice() of Python. This code doesn't work with truly Unicode strings (better to use a dstring or dchar[] for that, unless you want to walk the string every time you want to extract a random item): import std.stdio, std.random; void main() { immutable array = "abcdef"; immutable rndChar = array[uniform(0, $)]; writeln(rndChar); } Bye, bearophile
Feb 02 2013
On Saturday, 2 February 2013 at 09:59:07 UTC, bearophile wrote:SaltySugar:Thank you!My code:In Phobos there isn't something like random.choice() of Python. This code doesn't work with truly Unicode strings (better to use a dstring or dchar[] for that, unless you want to walk the string every time you want to extract a random item): import std.stdio, std.random; void main() { immutable array = "abcdef"; immutable rndChar = array[uniform(0, $)]; writeln(rndChar); } Bye, bearophile
Feb 02 2013