Click to See Complete Forum and Search --> : Random letters


druss
01-17-2003, 10:49 PM
I am trying to print random letters on the html page however the problem is that i do not want the same letter apear more than once in the page.

Can someone tell me how to print out the alphabet in a random form without the letter being repeated?

My working progress is below


-----------------------------------
@alpha2 = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

for ($i = 0; $i <= 25; $i++) {

$ran = rand($#alpha2);

$test[$i] = $alpha2[$ran];

$l = 0;

foreach $slot (@alpha2) {
if($slot ne $alpha2[$l]) { $alpha2[$l] = $line; }
$l++;
}
}
-----------------------------------


Thanks a heap
Goran Sterjov

jeffmott
01-18-2003, 12:01 AM
my @letters = 'a' .. 'z';

for (1 .. 26) {
&nbsp; my $offset = int(rand(@letters));
&nbsp; print splice(@letters, $offset, 1), "\n";
}

druss
01-18-2003, 06:04 AM
Thanks for that, it really helped. However now i have another little problem..

It seems when i replace the letter with a random letter it can collide by replacing the random letter later on. really confusing i know..

Here is the script

------------------------------------------
$text = "hello";

@letters = 'a' .. 'z';
$i = 0;
for (1 .. 26) {
$offset = int(rand(@letters));
$alpha2[$i] = splice(@letters, $offset, 1);
$i++;
}


@letters = 'a' .. 'z';

for ($i = 0; $i <= 25; $i++) {
$text =~ s/$letters[$i]/$alpha2[$i]/g;
}

print <<END;
content-type: text/html\n\n

<html>
$text
<br>
@alpha2
<br>
@letters

</html>

END

exit;
--------------------------------------------

See where it replaces? once i replace a with b, when i get around to replace b to h, it replaces the already replaced a...

Need help to get around the problem.

The script is in the following address to incase you need to see it: http://leechbuster.com/improv.cgi


Thanks
Goran

jeffmott
01-18-2003, 09:18 AM
my $text = 'hello';

my @randalpha;
my @letters = 'a' .. 'z';

for (1 .. 26) {
&nbsp; my $offset = int(rand(@letters));
&nbsp; push(@randalpha, splice(@letters, $offset, 1));
}

eval('$text =~ tr/a-z/' . join('', @randalpha) . '/');