get random char from array

bearophile bearophileHUGS at lycos.com
Sat Feb 2 01:59:06 PST 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


More information about the Digitalmars-d-learn mailing list