SoFunction
Updated on 2025-04-07

Perl code to generate random sequences of specific base ratios


#!/usr/bin/perl -w

use strict;

my @seq = ( "A", "T", "C", "G" );
my $length = 10000;

undef my %hash;
$hash{"A"} = int( $length * 0.3 );
$hash{"C"} = int( $length * 0.3 );
$hash{"G"} = int( $length * 0.2 );
$hash{"T"} = int( $length * 0.2 );

my $i = 0;
while ( $i 《 $length ) {
    my $word = $seq[ rand(@seq) ];
    if ( $hash{$word} ) {
        print "$word";
        $i++;
    }
    $hash{$word}--;
}
print "n";