Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: How to generate random sequence of UTF-8 characters

by davido (Archbishop)
on Dec 20, 2012 at 20:17 UTC ( #1009790=note: print w/replies, xml ) Need Help??

in reply to How to generate random sequence of UTF-8 characters

Bytes::Random::Secure: Most of this module deals with returning random bytes, but there is one function that returns a random string by picking octets at random from a user-supplied "bag" string. It works with Unicode strings as well (it turned out to be a simple thing to implement, despite the module's "Bytes" name).

use strict; use warnings; use Bytes::Random::Secure qw( random_string_from ); use feature qw/unicode_strings/; binmode STDOUT, ':utf8'; my $string = random_string_from( join( '', map { chr($_ ) } 0 .. 0x10FFFF ), # Generate a "bag" st +ring. 16384 # Return a string of 16384 octets from the "bag". ); print $string, "\n"; print length($string), "\n";

Not terribly efficient/, but if you precompute the "bag" string it's efficient enough for most purposes.

Care was taken to ensure that there's no modulo bias, which would be likely to turn up in simplisitic solutions. The random generator is the ISAAC algorithm. And the generator is seeded using the strongest source available on the target platform. Performance can be improved by making sure that Math::Random::ISAAC::XS is installed on the target system; the ISAAC generator will use it if it's available. On Windows systems there's one additional dependency to assure a strong seed. You'll have to read the POD if that's an issue.


Replies are listed 'Best First'.
Re^2: How to generate random sequence of UTF-8 characters
by ted.byers (Monk) on Dec 20, 2012 at 20:39 UTC

    Thanks Dave. I will take a look at that



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009790]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-02-24 13:34 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (354 votes). Check out past polls.