-
Notifications
You must be signed in to change notification settings - Fork 0
/
aleatorio.py
51 lines (35 loc) · 1.23 KB
/
aleatorio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# encoding: utf-8
import math
import rand_generator
# Classe para geracao de numeros aleatorios segundos varias distribuicoes
# Apenas a distribuicao exponencial negativa esta definida
def exponencial(media,seed):
# Gera um numero segundo uma distribuicao exponencial negativa de media m
return (-media * math.log(rand_generator.rand(seed)))
class Random:
stream_k = 0
def __init__(self, seed):
self.proxima_peca = 0
self.existe_peca = False
self.seed = seed
self.stream = Random.stream_k
rand_generator.randst(seed, self.stream)
Random.stream_k += 1
def normal(self, media,desvio):
if self.existe_peca:
self.existe_peca = False
return self.proxima_peca
v1 = 1
v2 = 2
while(v1**2 + v2**2 > 1):
v1 = 2 * rand_generator.rand(self.stream) - 1
v2 = 2 * rand_generator.rand(self.stream) - 1
z = v1**2 + v2**2
y1 = v1 * math.sqrt(-2 * math.log(z) / z)
y2 = v2 * math.sqrt(-2 * math.log(z) / z)
x1 = media + y1 * desvio
x2 = media + y2 * desvio
self.proxima_peca = x2
self.existe_peca = True
return (x1)