I am not much familiar with python..I found this small script in python I even do not have python on my computer...can anyone help me out to convert this into ksh or sh..
PLEASE any help I will appreciate..
here is python code..
#!/usr/bin/env python
import random # Get a random number generator.
NTRIALS = 10000 # Enough trials to get an reasonably accurate answer.
NPEOPLE = 30 # How many people in the group?
matches = 0 # Keep track of how many trials have matching birthdays.
for trial in range(NTRIALS): # Do a bunch of trials...
taken = {} # A place to keep track of which birthdays
# are already taken on this trial.
for person in range(NPEOPLE): # Put the peoples birthdays down, one at a time...
day = random.randint(0, 365) # On a randomly chosen day.
if day in taken:
matches += 1 # A match!
break # No need to look for more than one.
taken[day] = 1 # Mark the day as taken.
I wanted to see how does it work...I have no experience in python that is why I ask if anyone can help me out to compile it thru ksh or sh...
I would not like to install python..I would prefare if someone can help me out with his..becouse I plan to upgrade this method..and add new modules in shell..??
This Python script is very clear in what its supposed to do.
1) import random
This is a library to generate random numbers
In bash , you can use the RANDOM env variable
2)
NTRIALS = 10000
matches = 0
For these, they are the same as declaring shell variables
3)
for trial in range(NTRIALS): # Do a bunch of trials...
taken = {}
for person in range(NPEOPLE):
day = random.randint(0, 365)
if day in taken:
matches += 1
break # No need to look for more than one.
taken[day] = 1
range(NTRIALS) will give you a range of values from 0 to 9999
so for trial in range(NTRIALs) is a for loop to generate these numbers. So this for loop will iterate 10000 times, starting from count 0.
matches+=1 is the same as matches = matches + 1, which in shell, you can use something like this (or other methods)
x=`echo "$x + 1" | bc`
taken = {} declares a dictionary, or a lookup table.
random.randint(0,365) will generate a random number in the range from 0 to 365. taken[day] = 1 means keep that random day number in the dictionary
with the value of 1.
taken will now contain for example:
taken = { 102:1 } # 102 generated from random.randint()