ผลต่างระหว่างรุ่นของ "Probstat/coin toss experiments"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย '== One coin toss simulation == In this part, we will perform a few simulations on coin tosses. Below are example codes for generating...') |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 1: | แถว 1: | ||
− | |||
− | |||
In this part, we will perform a few simulations on coin tosses. Below are example codes for generating random numbers in Java and Python. You can definitely use other languages. | In this part, we will perform a few simulations on coin tosses. Below are example codes for generating random numbers in Java and Python. You can definitely use other languages. | ||
− | + | == Some code == | |
The following is a Java code for randomly tossing a fair coin for 100 times. | The following is a Java code for randomly tossing a fair coin for 100 times. | ||
แถว 45: | แถว 43: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | == Distributions == | |
== Number of trials == | == Number of trials == |
รุ่นแก้ไขเมื่อ 00:54, 19 สิงหาคม 2557
In this part, we will perform a few simulations on coin tosses. Below are example codes for generating random numbers in Java and Python. You can definitely use other languages.
Some code
The following is a Java code for randomly tossing a fair coin for 100 times.
import java.util.Random;
public class CoinToss {
public static void main(String[] args) {
Random random = new Random();
int N = 100;
int c = 0;
for (int i = 0; i < N; i++) {
boolean head = random.nextDouble() >= 0.5;
if (head) {
c++;
}
}
System.out.println("Heads: " + c);
System.out.println("Freq/N: " + ((double) c)/N);
}
}
And this is an equivalent one in Python.
import random
N = 100
c = 0
for i in range(N):
head = random.random() >= 0.5
if head:
c += 1
print "Heads:",c
print "Freq/N:",float(c)/N