| Thread overview | |||||
|---|---|---|---|---|---|
|
February 02, 2013 get random char from array | ||||
|---|---|---|---|---|
| ||||
My code:
import std.stdio;
import std.random;
void main()
{
}
| ||||
February 02, 2013 Re: get random char from array | ||||
|---|---|---|---|---|
| ||||
Posted in reply to SaltySugar | 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
| |||
February 02, 2013 Re: get random char from array | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 2 February 2013 at 09:59:07 UTC, bearophile wrote:
> 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
Thank you!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply