use 5.010; use strict; # A few definitions... use constant { ITERATIONS => 20, # number of random strings to generate LENGTH => 72, # length of each string X_COUNT => 4, # number of X characters in each string }; # We need a uniq() function. List::MoreUtils provides a fast XS based # one, but if they don't have it installed, fall back to a pure Perl # alternative. BEGIN { *uniq = eval { require List::MoreUtils; 1 } ? \&List::MoreUtils::uniq : sub (@) { my %seen = (); grep { not $seen{$_}++ } @_ }; } # We want to generate more than one random string. foreach my $iter (1 .. ITERATIONS) { # Make a pool of random numbers. uniq ensures no duplicates, # but means that we might end up with fewer than we need, # hence a loop to top up the pool if it's below the number # we need. my @positions; while (@positions < X_COUNT) { push @positions, map { int rand LENGTH } 0 .. X_COUNT; @positions = uniq @positions; } # Take just the first X_COUNT items from our pool, then # map it into a hash structure. my %exes = map { $_ => 1 } @positions[0 .. X_COUNT-1]; # Create our array of length LENGTH. Indexes found in the # %exes hash get an "X"; other indexes get a hyphen. my @array = map { $exes{$_} ? 'X' : '-' } 1 .. LENGTH; # Print out the array as a string. say join q{}, @array; }

In reply to Re: Difficulty in randomization by tobyink
in thread Difficulty in randomization by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.