Advertisement
PhiNotPi

Random Bit Sequence Tests

Aug 2nd, 2012
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.19 KB | None | 0 0
  1. $string = <>;  #the random bit string is taken as input from STDIN. As of now, it can't contain whitespace
  2. chomp($string);
  3. $string = reverse($string);
  4.  
  5. TEST2:{
  6.  $stra = $string;
  7.  for(1..240){
  8.   $bits = 0;
  9.   $bits += chop($stra);
  10.   $bits += chop($stra);
  11.   $bits += chop($stra);
  12.   if($bits == 0){
  13.    $zero++
  14.   }
  15.   if($bits == 1){
  16.    $one++
  17.   }
  18.   if($bits == 2){
  19.    $two++
  20.   }
  21.   if($bits == 3){
  22.    $three++
  23.   }
  24.  }
  25.  $zero *= 8/240;
  26.  $one *= 8/240;
  27.  $two *= 8/240;
  28.  $three *= 8/240;
  29.  print "Test 2: $zero (1) $one (3) $two (3) $three (1)\n";  #the proportions should be 1:3:3:1
  30. }
  31.  
  32. TEST3:{
  33.  $strb = $string;
  34.  $state = chop($strb);
  35.  $runlength = 1;
  36.  for(1..998){
  37.   $temp = chop($strb);
  38.   if($temp == $state){
  39.    $runlength++
  40.   }
  41.   else{
  42.    $runs[$runlength]++;
  43.    $state = $temp;
  44.    $runlength = 1;
  45.   }
  46.  }
  47.  print "Test 3: @runs\n"; #the result should be a series of numbers, each 1/2 of the previous number
  48. }
  49.  
  50. TEST3:{
  51.  $strc = $string;
  52.  for(0..209){
  53.   $half1[$_] = chop($strc);
  54.  }
  55.  for(0..209){
  56.   $half2[$_] = chop($strc);
  57.  }
  58.  for(0..209){
  59.   if($half1[$_] == $half2[$_]){
  60.    $same++;
  61.   }
  62.  }
  63.  $same /= 210;
  64.  print "Test 4: $same\n"; #the result should be 0.5
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement