in reply to Random data generation.

This seems to work, if I've understood correctly.

use strict; use warnings; use 5.010; say genString( 12, ( q{a} .. q{f} ) ); say genString( 20, ( q{a} .. q{c} ) ); say genString( 20, ( q{a} .. q{b} ) ); sub genString { my( $len, @set ) = @_; my $string = q{}; for ( 1 .. $len ) { if ( $string =~ m{(.)\1$} ) { my $let; do { $let = $set[ int rand @set ] } until $let ne $1; $string .= $let; } else { $string .= $set[ int rand @set ]; } } return $string; }

The output.

bceffcedefda bccbbcaabbccacabbcaa abaabbaabaabababbabb

You would need to add a test to prevent an endless loop if given a set with only one member.

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Random data generation.
by BrowserUk (Patriarch) on Jun 27, 2010 at 15:21 UTC

    A working solution, but like others that invoke the regex engine, it proves slower, especially for longer strings, than a flags & conditions solution. (Eg. salva's 846681).

    [16:16:52.15] c:\test>junk -I=3 ABCDEF 12 Rate x0 x3 x8 x4 x6 x5 x7 x0 9284/s -- -7% -16% -33% -38% -38% -47% x3 9946/s 7% -- -10% -29% -33% -34% -44% ikegami x8 11091/s 19% 12% -- -20% -26% -26% -37% johngg x4 13951/s 50% 40% 26% -- -7% -7% -21% ikegami variant x6 14952/s 61% 50% 35% 7% -- -0% -15% x5 14964/s 61% 50% 35% 7% 0% -- -15% x7 17647/s 90% 77% 59% 26% 18% 18% -- salva [16:17:13.48] c:\test>junk -I=3 ABCDEF 120 Rate x0 x3 x4 x8 x6 x5 x7 x0 279/s -- -8% -10% -78% -83% -83% -91% x3 302/s 8% -- -3% -76% -81% -81% -90% x4 310/s 11% 3% -- -75% -81% -81% -89% x8 1250/s 348% 314% 303% -- -22% -22% -58% x6 1595/s 472% 428% 414% 28% -- -0% -46% x5 1597/s 472% 429% 415% 28% 0% -- -46% x7 2951/s 958% 877% 851% 136% 85% 85% -- [16:17:30.86] c:\test>junk -I=3 ABCDEF 1200 Rate x0 x4 x3 x8 x6 x5 x7 x0 3.38/s -- -1% -3% -97% -98% -98% -99% x4 3.42/s 1% -- -1% -97% -98% -98% -99% x3 3.46/s 3% 1% -- -97% -98% -98% -99% x8 121/s 3478% 3433% 3388% -- -24% -24% -63% x6 159/s 4608% 4548% 4489% 32% -- -1% -51% x5 160/s 4633% 4573% 4513% 32% 1% -- -51% x7 324/s 9499% 9377% 9256% 168% 104% 103% --

    As you can see. salva's solution wins easily on performance.

      I wonder if you've overlooked my solution, or if there's anything wrong with it.

      Not only is it simpler (both less lines of code and conceptually easier to understand, IMHO), it's also consistently faster than what you found to be the fastest:

      $ ./846784.pl Rate salva almut salva 881/s -- -15% almut 1037/s 18% --

      (tested with v5.10.1, x86_64-linux-thread-multi)

        I wonder if you've overlooked my solution, or if there's anything wrong with it.

        Indeed I did, as it was posted as a reply to someone else. And no, there is nothing wrong with it.

        It beats out salva's (just), and is a simple, clean, and direct implementation of the requirements. I have my winner. Thank you.

        Now I've seen it, I'm somewhat chagrined that I didn't see it for myself. It would still be my winner even if it were slightly slower than the flags & resets versions.

        Yours is xA below:

        [19:48:48.65] c:\test>junk -I=3 ABCDEF 12 Rate x0 x3 x9 x8 x5 x4 x6 x7 xA x0 9794/s -- -8% -13% -19% -35% -36% -38% -43% -51% x3 10606/s 8% -- -6% -12% -30% -31% -33% -39% -46% x9 11286/s 15% 6% -- -6% -25% -26% -29% -35% -43% x8 12063/s 23% 14% 7% -- -20% -21% -24% -30% -39% x5 15122/s 54% 43% 34% 25% -- -1% -5% -12% -24% x4 15340/s 57% 45% 36% 27% 1% -- -3% -11% -23% x6 15863/s 62% 50% 41% 32% 5% 3% -- -8% -20% x7 17248/s 76% 63% 53% 43% 14% 12% 9% -- -13% xA 19822/s 102% 87% 76% 64% 31% 29% 25% 15% -- [19:49:50.63] c:\test>junk -I=3 ABCDEF 120 Rate x0 x3 x4 x8 x9 x6 x5 x7 xA x0 275/s -- -20% -21% -80% -80% -86% -86% -91% -92% x3 341/s 24% -- -2% -75% -75% -82% -83% -89% -90% x4 349/s 27% 2% -- -75% -75% -82% -82% -89% -90% x8 1383/s 404% 305% 296% -- -0% -29% -30% -57% -59% x9 1386/s 405% 306% 297% 0% -- -29% -30% -57% -59% x6 1945/s 608% 470% 457% 41% 40% -- -1% -40% -42% x5 1971/s 618% 477% 465% 43% 42% 1% -- -39% -41% x7 3216/s 1071% 842% 821% 133% 132% 65% 63% -- -4% xA 3346/s 1119% 880% 859% 142% 141% 72% 70% 4% -- [19:50:43.49] c:\test>junk -I=3 ABCDEF 1200 Rate x4 x0 x3 x8 x9 x6 x5 x7 +xA x4 3.33/s -- -0% -8% -98% -98% -98% -98% -99% -9 +9% x0 3.33/s 0% -- -8% -98% -98% -98% -98% -99% -9 +9% x3 3.61/s 8% 8% -- -97% -98% -98% -98% -99% -9 +9% x8 137/s 4019% 4019% 3700% -- -8% -25% -27% -61% -6 +3% x9 149/s 4366% 4366% 4020% 8% -- -19% -21% -58% -6 +0% x6 183/s 5392% 5392% 4966% 33% 23% -- -3% -48% -5 +0% x5 189/s 5578% 5578% 5138% 38% 27% 3% -- -46% -4 +9% x7 351/s 10434% 10434% 9618% 156% 136% 92% 86% -- - +5% xA 368/s 10960% 10960% 10104% 169% 148% 101% 95% 5% +--