Operating System - HP-UX
1834881 Members
2325 Online
110071 Solutions
New Discussion

Re: python code...convert to ksh or sh

 
SOLVED
Go to solution
amonamon
Regular Advisor

python code...convert to ksh or sh

Helloo...

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 people’s 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.
6 REPLIES 6
Peter Godron
Honored Contributor

Re: python code...convert to ksh or sh

Hi,
If you do not have python on your machine, you are not riunning the codse currently, so why do you want to port it ?

You can use the Random Number Generator (RNG)product:
http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I

Your python code comes from:
kochanski.org/gpk/./teaching/0501Oxford/MonteCarlo.pdf

Could you also please revisit your previous threads and tidy them up (award points and close), for example:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1086922


amonamon
Regular Advisor

Re: python code...convert to ksh or sh

thanks,

Bingo..yes my code comes from that page..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..??

Regards..any help is very welcome????
amonamon
Regular Advisor

Re: python code...convert to ksh or sh

right..I did close previus thread..sorryy that I forgot that one..

I still hope I will get more help on this one?!?
spex
Honored Contributor
Solution

Re: python code...convert to ksh or sh

Hi,

I remember discussing "the birthday" problem in one of my CS classes...

#!/usr/bin/sh

typeset -i NTRIALS=10000
typeset -i NPEOPLE=30
set -A TAKEN
typeset -i MATCHES=0
typeset -i CURTRIAL=0
typeset -i DAY

while [[ ${CURTRIAL} -lt ${NTRIALS} ]]
do
DAY=$(( ${RANDOM}%365 ))
if [[ ${TAKEN[${DAY}]:--1} -gt 0 ]]
then
MATCHES=$(( ${MATCHES}+1 ))
TAKEN[${DAY}]=$(( ${TAKEN[${DAY}]}+1 ))
else
TAKEN[${DAY}]=1
fi
#echo "Trial: ${CURTRIAL} Day: ${DAY} Matches: ${MATCHES}"
CURTRIAL=$(( ${CURTRIAL}+1 ))
done
echo "Matches: ${MATCHES}"
exit 0

PCS
amonamon
Regular Advisor

Re: python code...convert to ksh or sh

Thanks a lot...I do appreciate it..

Regards..
amonamon
Regular Advisor

Re: python code...convert to ksh or sh

Thanks everyone..