Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 14, 2014 Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Is there a natural way of generating/filling a string/wstring/dstring of a specific length with random contents? |
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> Is there a natural way of generating/filling a string/wstring/dstring of a specific length with random contents?
Do you mean something like this?
import std.stdio, std.random, std.ascii, std.range, std.conv;
string genRandomString(in size_t len) {
return len
.iota
.map!(_ => lowercase[uniform(0, $)])
.text;
}
void main() {
import std.stdio;
10.genRandomString.writeln;
}
Bye,
bearophile
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 14 July 2014 at 22:21:36 UTC, bearophile wrote:
> Nordlöw:
>
>> Is there a natural way of generating/filling a string/wstring/dstring of a specific length with random contents?
>
> Do you mean something like this?
>
>
> import std.stdio, std.random, std.ascii, std.range, std.conv;
>
> string genRandomString(in size_t len) {
> return len
> .iota
> .map!(_ => lowercase[uniform(0, $)])
> .text;
> }
>
> void main() {
> import std.stdio;
>
> 10.genRandomString.writeln;
> }
>
>
> Bye,
> bearophile
Alternative:
randomSample(lowercase, 10, lowercase.length).writeln;
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | On Monday, 14 July 2014 at 22:27:57 UTC, Brad Anderson wrote:
>
> Alternative:
>
> randomSample(lowercase, 10, lowercase.length).writeln;
std.ascii should really be using std.encoding.AsciiString. Then
that length wouldn't be necessary.
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | Brad Anderson: > Alternative: > > randomSample(lowercase, 10, lowercase.length).writeln; From randomSample docs: >Selects a random subsample out of r, containing exactly n elements. The order of elements is the same as in the original range.< Bye, bearophile |
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 14 July 2014 at 22:21:36 UTC, bearophile wrote:
> Nordlöw:
>
>> Is there a natural way of generating/filling a string/wstring/dstring of a specific length with random contents?
>
> Do you mean something like this?
>
>
> import std.stdio, std.random, std.ascii, std.range, std.conv;
>
> string genRandomString(in size_t len) {
> return len
> .iota
> .map!(_ => lowercase[uniform(0, $)])
> .text;
> }
>
> void main() {
> import std.stdio;
>
> 10.genRandomString.writeln;
> }
>
>
> Bye,
> bearophile
I was specifically interested in something that exercises (random samples) potentially _all_ code points for string, wstring and dstring (all code units that is).
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 14 July 2014 at 22:32:51 UTC, Nordlöw wrote: I believe defining a complete random sampling of all code units in dchar is a good start right? This can then be reused to lazily convert while filling in a string and wstring. |
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> I was specifically interested in something that exercises (random samples) potentially _all_ code points for string, wstring and dstring (all code units that is).
That's harder. Generating all uints and then testing if it's a Unicode dchar seems possible.
Bye,
bearophile
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 14 July 2014 at 22:35:59 UTC, Nordlöw wrote: > On Monday, 14 July 2014 at 22:32:51 UTC, Nordlöw wrote: > > I believe defining a complete random sampling of all code units in dchar is a good start right? This can then be reused to lazily convert while filling in a string and wstring. isValidCodePoint() at http://dlang.org/phobos/std_encoding.html might be were to start. |
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> I believe defining a complete random sampling of all code units in dchar is a good start right? This can then be reused to lazily convert while filling in a string and wstring.
Several combinations of unicode chars are not meaningful/valid (like pairs of ligatures). Any thing that has to work correctly with Unicode is complex.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation