ラベル Hobby の投稿を表示しています。 すべての投稿を表示
ラベル Hobby の投稿を表示しています。 すべての投稿を表示

2014年12月24日水曜日

ヘルスライスでバーサーク

『カザンの闘技場』でもらえるヘルスライス(42D)でバーサークするとどの位ヒットが出るのか?」という子供の頃の疑問に答えるスクリプトを誘惑に負けて作成。関数を再帰的に使うことの練習ということで(汗)。

100万回トライして
平均 :  357
標準偏差 :  23
最大 :  501
最小 :  268

非バーサーク時(期待値 147)の2.4倍。
分布でなんか変なヒゲが生えているのは何なのだろう...?



ハイパーバーサークだと1万回トライして
平均 :  499
標準偏差 :  51
最大 :  765
最小 :  348

...仕事します。

import random
import numpy
import matplotlib.pyplot as plt

def normal_dice(dice_num) :
    vals = []
    total_val = 0
    
    random.seed()
    for num in range(0,dice_num):
        vals.append(random.randint(1, 6))
    
    #print vals
    
    total_val = sum(vals)
    
    return total_val


def berserk_dice(dice_num) :
    vals = []
    total_val = 0
    
    random.seed()
    for num in range(0,dice_num):
        vals.append(random.randint(1, 6))
    
    #print vals
    
    total_val = total_val + sum(vals)
   
    for num in range(1,6):
        num_of_same = vals.count(num)
        if num_of_same > 1 :
            #print "Bersrk by %d dices of %d marks" % (num_of_same,num) 
            total_val=total_val+berserk_dice(num_of_same)
    
    return total_val

def hyper_berserk_dice(dice_num) :
    vals = []
    total_val = 0
    
    random.seed()
    for num in range(0,dice_num):
        vals.append(random.randint(1, 6))
    
    #print vals
    
    total_val = total_val + sum(vals)
   
    for num in range(1,6):
        num_of_same = vals.count(num)
        if num_of_same > 1 :
           # print "Bersrk by %d dices of %d marks" % (num_of_same,num) 
            total_val=total_val+hyper_berserk_dice(num_of_same+1)
    
    return total_val
    
def main():
    dice_num = input("Input dice_num :")
    trial = input("Input Num. of Trials :")
    vals=[]

    for num in range(0, trial) :
        vals.append(berserk_dice(dice_num)) 
        if num%10000==0:
            print num

    vals=numpy.array(vals)
    print "Mean : ", numpy.mean(vals)
    print "SD : ", numpy.std(vals)
    print "Max : ", vals.max()
    print "Min : ", vals.min()
   
    plt.rc('font', **{'family': 'serif'})
    fig = plt.figure()
    
    ax = fig.add_subplot(111)
    ax.hist(vals, bins=100)
    ax.set_title('Berserk with %d dices! (%d trials)' % (dice_num, trial))
    ax.set_xlabel('Hits!')
    ax.set_ylabel('Frequency')

    plt.show()    
    return None 
    
main()

2013年7月1日月曜日

Twitterで勝手に掛け合いをするBot的な何か

ついカッとなって、週末作った。
今は反省している。 (これをcrontabで定期的に走らせたりすると・・・ゴホゴホ)
勢いでやったからよく覚えていないがこれを使ったっぽい。

#!/Library/Frameworks/Python.framework/Versions/7.2/bin/python
# -*- coding: utf-8 -*-

SCRIPT_TYPE="InstrumentalityOfMankind" 
SCRIPT_SHELL="python" 
SCRIPT_NAME=""
SCRIPT_HELP_ARG=""
SCRIPT_HELP_SENTENCE=""
SCRIPT_NUM_ARG=0
SCRIPT_VERSION=1.0

###IMPORT###

import sys
import tweepy
import random
import time

###HELP###

if len(sys.argv[1:])!=SCRIPT_NUM_ARG:
    print 'Name: '+SCRIPT_NAME
    print 'Arguments: '+SCRIPT_HELP_ARG
    print 'Explanation: '+SCRIPT_HELP_SENTENCE
    sys.exit()


###MAIN###

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
logfile="temp.log"




###ふぁんくしょん###

def Tweets(ack, acs, text) : 
    CONSUMER_KEY = ''
    CONSUMER_SECRET = ''
 
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ack, acs)
    api = tweepy.API(auth, api_root='/1.1')
    api.update_status(text)

     for p in tweepy.Cursor(api.user_timeline).items(1):
          lastid=p.id
    print text
    del api  
    return lastid


def Tweets_as_A(text) :
    ACCESS_KEY1 = ''
    ACCESS_SECRET1 = ''
    return Tweets(ACCESS_KEY1, ACCESS_SECRET1, text)


def Tweets_as_B(text) :
    ACCESS_KEY2 = ''
    ACCESS_SECRET2 = '' 
    return Tweets(ACCESS_KEY2, ACCESS_SECRET2, text)


def Reply(ack, acs, text, lastid, to_usr) : 
    CONSUMER_KEY = ''
    CONSUMER_SECRET = ''
 
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ack, acs)
    api = tweepy.API(auth, api_root='/1.1')

    name_to_reply = to_usr
    status_id_to_reply = lastid
    text2 = u'@%s %s' % (name_to_reply, text)
    api.update_status(text2, in_reply_to_status_id = status_id_to_reply)
    print text2
    for p in tweepy.Cursor(api.user_timeline).items(1):
          a=p.id
    del api
    return a


def Reply_as_A(text,lastid) :
    ACCESS_KEY1 = ''
    ACCESS_SECRET1 = ''
    return Reply(ACCESS_KEY1, ACCESS_SECRET1, text, lastid, "B")


def Reply_as_B(text, lastid) :
    ACCESS_KEY2 = ''
    ACCESS_SECRET2 = '' 
    return Reply(ACCESS_KEY2, ACCESS_SECRET2, text, lastid, "A")

 

####おだいけってい###

num_wadai=2

f1=open(logfile,"r")
old_nums=[]
for line in f1:
   old_nums.append(line)
f1.close()

prev_num=int(old_nums[-1])

num_now=random.randint(1, num_wadai)
while prev_num == num_now  : 
    num_now=random.randint(1, num_wadai)
    print num_now, prev_num

f1=open(logfile,"w")
old_nums.append(str(num_now)+"\n")
f1.writelines(old_nums)
f1.close()


####おしゃべり#####


if num_now==1 :
    text=u"はーちみつ!"
    sid=Tweets_as_A(text)
 
    text=u"レモン!"
    sid=Reply_as_B(text, sid)
 
 
elif num_now==2 :
    text=u"レモンはー!"
    sid=Tweets_as_B(text)
 
    text=u"ハチミツ!"
    sid=Reply_as_A(text, sid)
 
    text=u"なんでやねん!"
    sid=Reply_as_B(text, sid)