(* The following example shows how functions are defined.*)

f[x_]:=x^2+1

f[20]

f[2]

(*RandomReal[] returns a random real number in the interval [0,1].*)

RandomReal[]

(* We define the function "draw" in such a manner that it returns "r" with probability 2/5, "w" with probability 2/5 and "b" with probability 1/5.*)

draw[]:=Module[
{x=RandomReal[]},
Which[
0≤x<2/5, "r",
2/5≤x<4/5, "w",
4/5≤x≤1, "b"]
]

(* We make a table of 10000 instances of draw[].*)

mydraws = Table[draw[], {10000}]

Tally[mydraws]

(*
Assignment.  

1) Modify the function draw so that the probabilities of the colors "r", "w" and "b" are 1/2, 1/3 and 1/6.

2) Modify the example so there are more than three colors.

*)

(***********************************************************************)

ranks={"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

suits={"Spades", "Hearts", "Clubs", "Diamonds"};

pickacard[]:=ranks[[RandomInteger[{1, 13}]]]<>" of "<> suits[[RandomInteger[{1,4}]]]

pickacard[]

Table[pickacard[], {5}]

deck=Flatten@Outer[#2<>" of "<>#1&, suits, ranks]

deal[n_(*number of cards*), p_(*number of players*)]:=Partition[RandomSample[deck, n*p],n]

deal[5,8]

(***********************************************************************)

flipbis[]:=Module[{xx={RandomInteger[]},len=1},
While[xx[[-1]]≠1,AppendTo[xx, RandomInteger[]];len++];
{xx,len}]

Table[flipbis[], {10}]

flip[]:=Module[{xx={RandomInteger[]},len=1},
While[xx[[-1]]≠1,AppendTo[xx, RandomInteger[]];len++];
len]

Sort@Tally[Table[flip[], {1000000}]]