• Sign Up
• Login
• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
89%
SHARE
TWEET

# Untitled

a guest May 19th, 2017 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. open Random;;
2.
3. let uniform () =
4.   Random.float 1.;;
5.
6. let bernouilli p =
7.   uniform() < p;;
8.
9. let discrete q =
10.   let rec discrete_rec r sum i q =
11.     match q with
12.     | [] -> i
13.     | h::l when r<sum -> i
14.     | h::l -> discrete_rec r sum+h i+1 l;;
15.   in
16.   begin
17.     let r = uniform();;
18.     discrete_rec r 0. 0 q;;
19.   end;;
20.
21. let exponential lambda =
22.   -1./.lambda log(1. -uniform());;
23.
24.
25. let random_bits p =
26.   let rec random_bits_rec n m p sum=
27.     match n with
28.     | 0 -> sum/.float_of_int m
29.     | _ -> begin
30.              let bit=bernoulli p;;
31.              printf "%B ";;
32.              random_bits_rec n-1 m+1 p sum+(int_of_bool bit)
33.            end;;
34.   in begin
35.     let res = random_bits_rec 30 0 p 0;;
36.     printf "\n Average: %g\n\n";;
37.      end;;
38.
39. let random_text q =
40.   let rec random_text_rec n m q sum =
41.     match n with
42.     | 0 -> begin
43.              let print_out k a =
44.                printf "%g " a/.(float_of_int k);;
45.              in
46.                Array.iter sum (print_out m)
47.            end
48.     | _ -> begin
49.              let letter=discrete q;;
50.              match letter with
51.              | 0 -> printf "  ";
52.              | 1 -> printf "l ";
53.              | 2 -> printf "b ";
54.              | 3 -> printf "a ";
55.              sum.(letter)=sum.(letter)+1;;
56.            end
57.   in
58.     random_text_rec 30 0 q (Array.new (List.length q) 0);;
59.
60. let p = 0.8;;
61. let q = [.1; .2; .5; .2];;
62. random_bits p;;
63. random_text q;;
RAW Paste Data
Top