July 17, 2014 Re: Generating Strings with Random Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | On Wednesday, 16 July 2014 at 23:24:24 UTC, Joseph Rushton Wakeling via Digitalmars-d-learn wrote: > 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? Just a random dchar (ciode point) sample like this: /** Generate Random Contents of $(D x). See also: http://forum.dlang.org/thread/emlgflxpgecxsqweauhc@forum.dlang.org */ auto ref randInPlace(ref dchar x) @trusted { auto ui = uniform(0, 0xD800 + (0x110000 - 0xE000) - 2 // minus two for U+FFFE and U+FFFF ); if (ui < 0xD800) { return x = ui; } else { ui -= 0xD800; ui += 0xE000; // skip undefined if (ui < 0xFFFE) return x = ui; else ui += 2; assert(ui < 0x110000); return x = ui; } } I don't know how well this plays with unittest { import dbg; dln(randomized!dchar); dstring d = "alphaalphaalphaalphaalphaalphaalphaalphaalphaalpha"; dln(d.randomize); } though. See complete logic at https://github.com/nordlow/justd/blob/master/random_ex.d |
Copyright © 1999-2021 by the D Language Foundation