shell
#!/bin/bash
x=0;y=0;a=0;number=1000
while (( $a <= $number ))
do
let "a++"
var=$[$RANDOM%2]
if [ $var -eq 1 ];
then
x=$(expr $x + 1)
elif [ $var -eq 0 ];
then
y=$(expr $y + 1)
else
echo $var
fi
done
echo $x;echo "scale=2;$x/$number"|bc
echo $y;echo "scale=2;$y/$number"|bc
python
#!/usr/bin/python
#! coding:utf-8
import random,numpy
a=0;b=0
for i in range(10000):
myarray = numpy.random.randint(0, 2)
if myarray == 1:
a += 1
if myarray == 0:
b += 1
print(a,b)
java
public class Coin {
public static void main(String[] args) {
int z=0; //记录正面次数
int f=0; //记录反面次数
for(int i=0;i<10000000;i++){
if((int)(Math.random()*2)==0){
z++; //产生随机数为0时 正面出现次数+1 否则出现的为反面
}else{
f++;
}
}
System.out.println("正面出现次数"+z);
System.out.println("反面出现次数"+f);
}
}
样本数值越大的时候运行 java>python>shell