July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 14 July 2014 at 22:39:08 UTC, Nordlöw wrote:
> might be were to start.
Is it really this simple?
bool isValidCodePoint(dchar c)
{
return c < 0xD800 || (c >= 0xE000 && c < 0x110000);
}
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 14 July 2014 at 22:39:15 UTC, bearophile wrote:
> Several combinations of unicode chars are not meaningful/valid (like pairs of ligatures). Any thing that has to work correctly with Unicode is complex.
So I guess we need something more than just isValidCodePoint right?
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 14 July 2014 at 22:32:25 UTC, bearophile wrote:
> 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
Hmm, good catch. Not the behavior I expected.
|
July 14, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Monday, 14 July 2014 at 22:45:29 UTC, Nordlöw wrote: > So I guess we need something more than just isValidCodePoint right? Here's a first try: https://github.com/nordlow/justd/blob/master/random_ex.d#L53 |
July 15, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> https://github.com/nordlow/justd/blob/master/random_ex.d#L53
Isn't @trusted mostly for small parts of Phobos code? I suggest to avoid using @trusted in most cases.
Bye,
bearophile
|
July 15, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tuesday, 15 July 2014 at 00:03:04 UTC, bearophile wrote:
> to avoid using @trusted in most cases.
Could someone elaborate shortly which cases this means?
|
July 15, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | Nordlöw:
> Could someone elaborate shortly which cases this means?
All cases where you really can't live without it :-) It's like a cast(.
Bye,
bearophile
|
July 15, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tuesday, 15 July 2014 at 18:50:06 UTC, bearophile wrote:
> All cases where you really can't live without it :-) It's like
Hmm. I guess I'm gonna have to remove some @trusted tagging then ;)
|
July 16, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On 15/07/14 00:16, "Nordlöw" via Digitalmars-d-learn wrote:
> Is there a natural way of generating/filling a string/wstring/dstring of a
> specific length with random contents?
I think you need to be more specific about what kind of random contents you are interested in having.
Are you interested in having each character in the sequence randomly chosen independently of all the others, or do you want a random subset of all available characters (i.e. no character appears more than once), or something else again?
|
July 16, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | > Alternative:
>
> randomSample(lowercase, 10, lowercase.length).writeln;
No, I don't think that's appropriate, because it will pick 10 individual characters from a, b, c, ... , z (i.e. no character will appear more than once), and the characters picked will appear in alphabetical order.
Incidentally, if lowercase has the .length property, there's no need to pass the length separately to randomSample. Just passing lowercase itself and the number of sample points desired is sufficient.
|
Copyright © 1999-2021 by the D Language Foundation