Skip to content

Instantly share code, notes, and snippets.

@yoshiki
Last active December 31, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshiki/7906409 to your computer and use it in GitHub Desktop.
Save yoshiki/7906409 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl \
use strict;
my $DEBUG = 0;
main();
sub main {
my $win = 0;
my $lose = 0;
my $total_times = 0;
my $num_of_sim = 10000;
for my $i ( 0..$num_of_sim-1 ) {
my ( $times, $is_win ) = simulate();
$total_times += $times;
if ( $is_win ) {
$win++;
} else {
$lose++;
}
}
print "WIN: $win\n";
print "LOSE: $lose\n";
print "RATE: " . ($win/$num_of_sim) * 100 . "%\n";
print "TIMES(AVG): " . $total_times/$num_of_sim . "\n";
}
sub simulate {
my $goal_coins = 5000; # 目標のコイン数
my $own_coins = 1000; # 手持ちのコイン
my $bet_coins = 40; # ベットするコイン数
my $num_bet = 12; # 何枠にベットするか
my $total_case = 37; # 当たる可能性のある枠
my $hit_rate = $num_bet/$total_case; # 単純計算した勝率
my $power = 36; # ベットした枠の倍率
my $times = 0;
while ( $own_coins < $goal_coins && $own_coins > 0 ) {
my $total_bet = $bet_coins * $num_bet;
$own_coins -= $total_bet;
if ( rand(1) > 1 - $hit_rate ) {
my $gain = $bet_coins * $power;
print "--- GAIN: $gain\n" if $DEBUG;
$own_coins += $gain;
} else {
print "--- LOSS: $total_bet\n" if $DEBUG;
}
print "CURRENT COIN: $own_coins\n" if $DEBUG;
$times++;
}
printf "=== FINISH ===\n" if $DEBUG;
return ( $times, ($own_coins >= $goal_coins) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment