Created
October 29, 2015 19:06
-
-
Save drusepth/7b85854561d8c9e9beb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# randinfo.pl | |
# by Drusepth 10/05/2007 | |
# Purpose: | |
# When using random numbers in programs, sometimes you want them to be | |
# actually random. But other times, (for example, if you were coding a | |
# lottery machine) you would want to know the probability a user would | |
# hit a jackpot (three consecutive 7s). This program calculates the | |
# percentage of NUMBER passed returning as a result of rand() with the | |
# passed SEED. With the information recieved, a programmer could decide | |
# on a seed that would be more fit for what they were writing. | |
$seed = shift or die | |
'---------------------------------------------------------------------- | |
Usage: perl randinfo.pl SEED [NUMBER] | |
Where SEED is the seed used to seed the rand() function. | |
and NUMBER is the number to look for the percentage of being a | |
result. | |
If NUMBER is omitted, the script will write the possibilities of the | |
numbers 0 through a set number ($max_omitted) to a text file | |
specified in the code as $output_file. | |
If $output_file exists at the time of running, it will be overwritten. | |
----------------------------------------------------------------------' | |
. "\n"; | |
$number = shift or $write = 1; | |
# Seed the random numbers: | |
srand($seed); | |
# Tweaks: # | |
# When generating random numbers, this will be the max result possible. | |
$max_number = 100; | |
# This is the output file to use if writing to a file. | |
$output_file = "randinfo-" . $seed . ".txt"; | |
# When a number to look for is omitted, look at all numbers between | |
# this number and 0. If this is above $max_number, it will be | |
# set to equal $max_number upon runtime. | |
$max_omitted = 100; | |
# When gathering the probability of recieving a number, this is the | |
# number of random numbers to generate, multiplied by $max_number. | |
# The higher the number, the more accurate the results, but it may | |
# also take a longer time for the script to run. | |
# Reasonable settings are 10 to 10000. | |
$repeat = 1000; | |
# This is the number of decimal places to go to when printing the results. | |
# Leave blank for no rounding. | |
$decimal_fix = 6; | |
if ($write == 1) { | |
print "Working...\n"; | |
# Find probabilities of numbers 1 through $max_omitted and write | |
# to $output_file. | |
$max_omitted++; | |
$max_number++; | |
# Make sure the number of numbers to check isn't more than the | |
# max number that a random number can be. | |
if ($max_omitted > $max_number) { | |
$max_omitted = $max_number; | |
} | |
# First, initialize the results array. | |
for ($i = 0; $i < $max_omitted; $i++) { | |
$results[$i] = 0; | |
} | |
# Then, generate random numbers inside the for loops and increment | |
# the corresponding value in @results. | |
for ($j = 0; $j < ($repeat * $max_number); $j++) { | |
$random = int(rand($max_number)); | |
$results[$random]++; | |
} | |
# The results array is full. Output to the terminal | |
$i = 0; | |
# Print the header: | |
print "Number\t-\tResults\t-\tPercent\n"; | |
# Put everything to be printed in $string. | |
foreach $result (@results) { | |
$percent = ($result / (($max_number * $repeat) + 0.5) * 100); | |
$percent = substr($percent, 0, $decimal_fix + 2); | |
$string .= "$i\t-\t$result\t-\t$percent%\n"; | |
$i++; | |
} | |
# Find the most- and least- used numbers and add them to $string. | |
# The least values are set high intentionally. | |
$max_i = 0; | |
$max_number = 0; | |
$least_i = 100000000000; | |
$least_number = 1000000000000; | |
foreach $result (@results) { | |
if ($result > $max_number) { | |
$max_i = $i; | |
$max_number = $result; | |
} | |
if ($result < $least_number) { | |
$least_i = $i; | |
$least_number = $result; | |
} | |
$i++; | |
} | |
$string .= "\n-------------------------------------------------\n"; | |
$string .= "Result Statistics:\n"; | |
$string .= "Most Used: $max_i at $max_number occurences.\n"; | |
$string .= "Least Used: $least_i at $least_number occurences.\n"; | |
$string .= "-------------------------------------------------\n\n"; | |
# Print to the file | |
print $string; | |
$error = "*** Error: Couldn't open/create $output_file for writing!\n"; | |
if (open(RESULTS, $output_file)) { | |
$i = 0; | |
foreach $result (@results) { | |
$percent = ($result/(($max_number*$repeat)+.5)*100); | |
$percent = substr($percent, 0, $decimal_fix + 2); | |
print "$string"; | |
$i++; | |
} | |
close(RESULTS); | |
} else { | |
print $error; | |
} | |
} else { | |
print "Working...\n"; | |
# Find how often a number occurs in the given seed. | |
# Do ittt | |
$occurences = 0; | |
for ($i = 0; $i < 1000000; $i++) { | |
if (int(rand($number * 100)) == $number) { | |
$occurences++; | |
} | |
} | |
$num_inc = $number * 100; | |
# Print to the terminal | |
$string = "When generating one million random numbers with the seed $seed | |
and a max result of $num_inc, $number occurs $occurences times. "; | |
$string .= "(" . ($occurences / 1000000) * 100 . "%)\n"; | |
print $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment