Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

r""" 

Random Numbers with Python API 

 

AUTHORS: 

-- Carl Witty (2008-03): new file 

 

This module has the same functions as the Python standard module 

\module{random}, but uses the current \sage random number state from 

\module{sage.misc.randstate} (so that it can be controlled by the same 

global random number seeds). 

 

The functions here are less efficient than the functions in \module{random}, 

because they look up the current random number state on each call. 

 

If you are going to be creating many random numbers in a row, it is 

better to use the functions in \module{sage.misc.randstate} directly. 

 

Here is an example: 

 

(The imports on the next two lines are not necessary, since 

\function{randrange} and \function{current_randstate} are both available 

by default at the \code{sage:} prompt; but you would need them 

to run these examples inside a module.) :: 

 

sage: from sage.misc.prandom import randrange 

sage: from sage.misc.randstate import current_randstate 

sage: def test1(): 

....: return sum([randrange(100) for i in range(100)]) 

sage: def test2(): 

....: randrange = current_randstate().python_random().randrange 

....: return sum([randrange(100) for i in range(100)]) 

 

Test2 will be slightly faster than test1, but they give the same answer:: 

 

sage: with seed(0): test1() 

5169 

sage: with seed(0): test2() 

5169 

sage: with seed(1): test1() 

5097 

sage: with seed(1): test2() 

5097 

sage: timeit('test1()') # random 

625 loops, best of 3: 590 us per loop 

sage: timeit('test2()') # random 

625 loops, best of 3: 460 us per loop 

 

The docstrings for the functions in this file are mostly copied from 

Python's \file{random.py}, so those docstrings are "Copyright (c) 

2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation; 

All Rights Reserved" and are available under the terms of the 

Python Software Foundation License Version 2. 

""" 

 

# We deliberately omit "seed" and several other seed-related functions... 

# setting seeds should only be done through sage.misc.randstate . 

 

from sage.misc.randstate import current_randstate 

 

def _pyrand(): 

r""" 

A tiny private helper function to return an instance of 

random.Random from the current \sage random number state. 

Only for use in prandom.py; other modules should use 

current_randstate().python_random(). 

 

EXAMPLES:: 

 

sage: from sage.misc.prandom import _pyrand 

sage: _pyrand() 

<random.Random object at 0x...> 

sage: _pyrand().getrandbits(10) 

114L 

""" 

return current_randstate().python_random() 

 

def getrandbits(k): 

r""" 

getrandbits(k) -> x. Generates a long int with k random bits. 

 

EXAMPLES:: 

 

sage: getrandbits(10) 

114L 

sage: getrandbits(200) 

1251230322675596703523231194384285105081402591058406420468435L 

sage: getrandbits(10) 

533L 

""" 

return _pyrand().getrandbits(k) 

 

def randrange(start, stop=None, step=1): 

r""" 

Choose a random item from range(start, stop[, step]). 

 

This fixes the problem with randint() which includes the 

endpoint; in Python this is usually not what you want. 

 

EXAMPLES:: 

 

sage: randrange(0, 100, 11) 

11 

sage: randrange(5000, 5100) 

5051 

sage: [randrange(0, 2) for i in range(15)] 

[0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1] 

sage: randrange(0, 1000000, 1000) 

486000 

sage: randrange(-100, 10) 

-56 

""" 

return _pyrand().randrange(start, stop, step) 

 

def randint(a, b): 

r""" 

Return random integer in range [a, b], including both end points. 

 

EXAMPLES:: 

 

sage: [randint(0, 2) for i in range(15)] 

[0, 1, 0, 0, 1, 0, 2, 0, 2, 1, 2, 2, 0, 2, 2] 

sage: randint(-100, 10) 

-46 

""" 

return _pyrand().randint(a, b) 

 

def choice(seq): 

r""" 

Choose a random element from a non-empty sequence. 

 

EXAMPLES:: 

 

sage: [choice(list(primes(10, 100))) for i in range(5)] 

[17, 47, 11, 31, 47] 

""" 

return _pyrand().choice(seq) 

 

def shuffle(x, random=None): 

r""" 

x, random=random.random -> shuffle list x in place; return None. 

 

Optional arg random is a 0-argument function returning a random 

float in [0.0, 1.0); by default, the sage.misc.random.random. 

 

EXAMPLES:: 

 

sage: shuffle([1 .. 10]) 

""" 

if random is None: 

random = _pyrand().random 

return _pyrand().shuffle(x, random) 

 

def sample(population, k): 

r""" 

Choose k unique random elements from a population sequence. 

 

Return a new list containing elements from the population while 

leaving the original population unchanged. The resulting list is 

in selection order so that all sub-slices will also be valid random 

samples. This allows raffle winners (the sample) to be partitioned 

into grand prize and second place winners (the subslices). 

 

Members of the population need not be hashable or unique. If the 

population contains repeats, then each occurrence is a possible 

selection in the sample. 

 

To choose a sample in a range of integers, use xrange as an 

argument (in Python 2) or range (in Python 3). This is especially 

fast and space efficient for sampling from a large population: 

sample(range(10000000), 60) 

 

EXAMPLES:: 

 

sage: sample(["Here", "I", "come", "to", "save", "the", "day"], 3) 

['Here', 'to', 'day'] 

sage: from six.moves import range 

sage: sample(range(2^30), 7) 

[357009070, 558990255, 196187132, 752551188, 85926697, 954621491, 624802848] 

""" 

return _pyrand().sample(population, k) 

 

def random(): 

r""" 

Get the next random number in the range [0.0, 1.0). 

 

EXAMPLES:: 

 

sage: [random() for i in [1 .. 4]] 

[0.111439293741037, 0.5143475134191677, 0.04468968524815642, 0.332490606442413] 

""" 

return _pyrand().random() 

 

def uniform(a, b): 

r""" 

Get a random number in the range [a, b). 

 

Equivalent to \code{a + (b-a) * random()}. 

 

EXAMPLES:: 

 

sage: uniform(0, 1) 

0.111439293741037 

sage: uniform(e, pi) 

0.5143475134191677*pi + 0.48565248658083227*e 

sage: RR(_) 

2.93601069876846 

""" 

return _pyrand().uniform(a, b) 

 

def betavariate(alpha, beta): 

r""" 

Beta distribution. 

 

Conditions on the parameters are alpha > 0 and beta > 0. 

Returned values range between 0 and 1. 

 

EXAMPLES:: 

 

sage: betavariate(0.1, 0.9) 

9.75087916621299e-9 

sage: betavariate(0.9, 0.1) 

0.941890400939253 

""" 

return _pyrand().betavariate(alpha, beta) 

 

def expovariate(lambd): 

r""" 

Exponential distribution. 

 

lambd is 1.0 divided by the desired mean. (The parameter would be 

called "lambda", but that is a reserved word in Python.) Returned 

values range from 0 to positive infinity. 

 

EXAMPLES:: 

 

sage: [expovariate(0.001) for i in range(3)] 

[118.152309288166, 722.261959038118, 45.7190543690470] 

sage: [expovariate(1.0) for i in range(3)] 

[0.404201816061304, 0.735220464997051, 0.201765578600627] 

sage: [expovariate(1000) for i in range(3)] 

[0.0012068700332283973, 8.340929747302108e-05, 0.00219877067980605] 

""" 

return _pyrand().expovariate(lambd) 

 

def gammavariate(alpha, beta): 

r""" 

Gamma distribution. Not the gamma function! 

 

Conditions on the parameters are alpha > 0 and beta > 0. 

 

EXAMPLES:: 

 

sage: gammavariate(1.0, 3.0) 

6.58282586130638 

sage: gammavariate(3.0, 1.0) 

3.07801512341612 

""" 

return _pyrand().gammavariate(alpha, beta) 

 

def gauss(mu, sigma): 

r""" 

Gaussian distribution. 

 

mu is the mean, and sigma is the standard deviation. This is 

slightly faster than the normalvariate() function, but is not 

thread-safe. 

 

EXAMPLES:: 

 

sage: [gauss(0, 1) for i in range(3)] 

[0.9191011757657915, 0.7744526756246484, 0.8638996866800877] 

sage: [gauss(0, 100) for i in range(3)] 

[24.916051749154448, -62.99272061579273, -8.1993122536718...] 

sage: [gauss(1000, 10) for i in range(3)] 

[998.7590700045661, 996.1087338511692, 1010.1256817458031] 

""" 

return _pyrand().gauss(mu, sigma) 

 

def lognormvariate(mu, sigma): 

r""" 

Log normal distribution. 

 

If you take the natural logarithm of this distribution, you'll get a 

normal distribution with mean mu and standard deviation sigma. 

mu can have any value, and sigma must be greater than zero. 

 

EXAMPLES:: 

 

sage: [lognormvariate(100, 10) for i in range(3)] 

[2.9410355688290246e+37, 2.2257548162070125e+38, 4.142299451717446e+43] 

""" 

return _pyrand().lognormvariate(mu, sigma) 

 

def normalvariate(mu, sigma): 

r""" 

Normal distribution. 

 

mu is the mean, and sigma is the standard deviation. 

 

EXAMPLES:: 

 

sage: [normalvariate(0, 1) for i in range(3)] 

[-1.372558980559407, -1.1701670364898928, 0.04324100555110143] 

sage: [normalvariate(0, 100) for i in range(3)] 

[37.45695875041769, 159.6347743233298, 124.1029321124009] 

sage: [normalvariate(1000, 10) for i in range(3)] 

[1008.5303090383741, 989.8624892644895, 985.7728921150242] 

""" 

return _pyrand().normalvariate(mu, sigma) 

 

def vonmisesvariate(mu, kappa): 

r""" 

Circular data distribution. 

 

mu is the mean angle, expressed in radians between 0 and 2*pi, and 

kappa is the concentration parameter, which must be greater than or 

equal to zero. If kappa is equal to zero, this distribution reduces 

to a uniform random angle over the range 0 to 2*pi. 

 

EXAMPLES:: 

 

sage: [vonmisesvariate(1.0r, 3.0r) for i in range(1, 5)] # abs tol 1e-12 

[0.898328639355427, 0.6718030007041281, 2.0308777524813393, 1.714325253725145] 

""" 

return _pyrand().vonmisesvariate(mu, kappa) 

 

def paretovariate(alpha): 

r""" 

Pareto distribution. alpha is the shape parameter. 

 

EXAMPLES:: 

 

sage: [paretovariate(3) for i in range(1, 5)] 

[1.0401699394233033, 1.2722080162636495, 1.0153564009379579, 1.1442323078983077] 

""" 

return _pyrand().paretovariate(alpha) 

 

def weibullvariate(alpha, beta): 

r""" 

Weibull distribution. 

 

alpha is the scale parameter and beta is the shape parameter. 

 

EXAMPLES:: 

 

sage: [weibullvariate(1, 3) for i in range(1, 5)] 

[0.49069775546342537, 0.8972185564611213, 0.357573846531942, 0.739377255516847] 

""" 

return _pyrand().weibullvariate(alpha, beta)