File accompanying lecture 5-1.

Christopher Hanusa

The RandomInteger and RandomReal commands.

RandomInteger[]

CoinFlips=RandomInteger[{0,1},20]
Total[CoinFlips]

If statements and For loops

x=3;
If[x<0,-x,x]

For[i=0,i<4,i++,Print[i]]

Simulating a coin toss

For[i=1,i≤20,i++,If[RandomInteger[]==1,Print["Head"],Print["Tail"]]]

headCount=0;tailCount=0;
For[i=1,i≤20,i++,If[RandomInteger[]==1,Print["Head"];headCount++,Print["Tail"];tailCount++]]
{headCount,tailCount}

Rolling a biased die

aCount=0;bCount=0;cCount=0;dCount=0;
For[i=1,i≤20,i++,roll=RandomReal[{0,1}];If[0≤roll<1/2,Print["a"];aCount++];
If[1/2≤roll<3/4,Print["b"];bCount++];
If[3/4≤roll<7/8,Print["c"];cCount++];
If[7/8≤roll≤1,Print["d"];dCount++];
]
{aCount,bCount,cCount,dCount}

Table[
aCount=0;bCount=0;cCount=0;dCount=0;
For[i=1,i≤20,i++,roll=RandomReal[{0,1}];If[0≤roll<1/2,aCount++];
If[1/2≤roll<3/4,bCount++];
If[3/4≤roll<7/8,cCount++];
If[7/8≤roll≤1,dCount++];
];
{aCount,bCount,cCount,dCount},
{j,1,20}]

TableForm[%]