2012/05/17

The Labs.Com Issue_04_Randomness
Last update 1999/02/20

TPJ: Issue_04_Randomness

This is a collection of programs published by The Perl Journal. You can download all source-code also from TPJ: Programs.
  1. ansirand
  2. lfsr
  3. More Samples on Randomness
Issue_04_Randomness
1. ansirand
Download ansirand

 #!/usr/bin/perl -w 
 use Config; 
 print "Your random number generator repeats itself after\n"; 
 print "no more than ", 2 ** $Config{randbits}, " numbers.\n"; 
  
 srand(1); 
  
 if (int(rand() * (2 ** $Config{randbits})) == 16838) { 
     print "Uh oh!  Looks like your computer uses the ANSI example.\n"; 
     print "I bet the next three numbers are 5758, 10113, and 17515.\n"\ 
 ; 
     foreach (1,2,3) { 
         print rand() * (2 ** $Config{randbits}), "\n"; 
     } 
 } 

Issue_04_Randomness
2. lfsr

Download lfsr

 #!/usr/bin/perl -w 
 $seed = dec2bin(shift); 
 @array = split('', $seed); 
  
 @xor_bits = @ARGV; 
  
 splice(@array, 0, 31-$ARGV[0]); 
 print "@array\n"; 
 while (1) { 
     getc(); 
     $new_bit = 0 + $array[0]; 
     foreach (@xor_bits[1..$#xor_bits]) { 
         $new_bit ^= $array[$#array - $_]; 
     } 
     print pop @array; 
     unshift (@array, $new_bit); 
 } 
  
 sub dec2bin { unpack("B*", pack("N", shift)) } 

Issue_04_Randomness
3. More Samples on Randomness

  • Issue_04_Randomness

                                                                                                                                   

Hipocrisy of the finest: "I agree that no single company can create all the hardware and software. Openness is central because it's the foundation of choice."
-- Steve Balmer (Microsoft) blaming Apple regarding iPhone, February 18, 2009

Last update 1999/02/20

All Rights Reserved - (C) 1997 - 2009 by The Labs.Com

Top of Page

The Labs.Com