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

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

by davido (Cardinal)
on Dec 20, 2012 at 20:17 UTC ( [id://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.


Dave

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

    Thanks

    Ted

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1009790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found