Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Generating mulitple random strings of a set length

by Kajed (Initiate)
on May 08, 2015 at 05:19 UTC ( [id://1126062]=perlquestion: print w/replies, xml ) Need Help??

Kajed has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to generate random strings from a set string with all of them being the same length and can be found already present inside the known string.

my $DNA = "AACCGTTAATGGGCATCGATGCTATGCGAGCT"; $DNA_length = length($DNA); my $random_sequence = &random_sequence($DNA); my $random_position = &random_position($DNA_length); my $random_base = &mutate(); $DNA[$random_sequence] = $DNA; my @DNA_array = split("", $DNA); print "$random_position\t$random_base\n"; $DNA_array[$random_position] = $random_base; print join("", @DNA_array), "\n"; sub random_sequence { my $sequence = m/\w*[ACTG]{5}*/; my $new_sequence = int(rand(scalar $sequence) return $sequence[$new_sequence]; sub random_position { my ($seed) = @_; return int(rand($seed)); } sub mutate { my @base_array = ('A','C','G','T'); my $random_base = int(rand(scalar @base_array)); return $base_array[$random_base]; }

I can get the random replacement but i cannot get the random sequence that the replacement is supposed to occur in. I have searched for a similar problem but none that I found helped solve the problem.

Replies are listed 'Best First'.
Re: Generating mulitple random strings of a set length
by thomas895 (Deacon) on May 08, 2015 at 05:23 UTC

    Have you even tried running your own code? How about use strict? Or use diagnostics?

    To be specific, look at lines 3, 6, 9, and 16 of your sample.

    -Thomas
    "Excuse me for butting in, but I'm interrupt-driven..."
Re: Generating mulitple random strings of a set length
by aaron_baugher (Curate) on May 08, 2015 at 10:01 UTC

    I don't understand what your code is trying to do or why you talk about "replacements," but if you want to get a random substring from a longer string, why not just pick a random offset between 0 and the difference between the full string's length and the substring's length? Here's an iterator which does that:

    #!/usr/bin/env perl use 5.010; use warnings; use strict; my $DNA = "AACCGTTAATGGGCATCGATGCTATGCGAGCT"; sub make_rand_getter { my $s = shift; my $sl = length $s; return sub { return substr $s, int rand($sl - $_[0]), $_[0]; } } my $rstring = make_rand_getter($DNA); say "3-letter string: " . $rstring ->(3); say "7-letter string: " . $rstring ->(7);

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Re: Generating mulitple random strings of a set length
by vinoth.ree (Monsignor) on May 08, 2015 at 10:45 UTC
    1.where is your random_sequence function ending curly braces ? 
    2.what you are trying to do in this line my $sequence = m/\w*[ACTG]{5}*/;
    3.$DNA argument passed to the random_sequence function but not used in function.
    4.$DNA get assigned scalar value and later accessing as array in this line $DNA[$random_sequence] = $DNA;
    

    All is well. I learn by answering your questions...
Re: Generating mulitple random strings of a set length
by edimusrex (Monk) on May 08, 2015 at 14:36 UTC
    This could be one way to do it with less code.

    #!/usr/bin/perl use warnings; use strict; my $DNA = "AACCGTTAATGGGCATCGATGCTATGCGAGCT"; my @rand = split("", $DNA); my $num = @rand; my $max = 5; my @word; for (my $i=0; $i<$max; $i++) { my $x = int(rand($num))-1; push @word, $rand[$x]; } print @word;

    By setting the variable $max to the desired length of the out put string.<br/
    Of course if you wanted to use the random word as a variable substitute the "print @word" with
    my $ranWord = join("",@word); print $ranWord;

    or something along those lines to create a variable from the array contents

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1126062]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found