summaryrefslogtreecommitdiff
path: root/test/ruby/test_rand.rb
blob: 2f2e772c1b0b9c10fb792cb8ce1e9fd00c370b68 (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
require 'test/unit'

class TestRand < Test::Unit::TestCase
  def assert_random_int(ws, m, init = 0)
    srand(init)
    rnds = [Random.new(init)]
    ws.each do |w|
      w = w.to_i
      assert_equal(w, rand(m))
      rnds.each do |rnd|
        assert_equal(w, rnd.int(m))
      end
      rnds << Marshal.load(Marshal.dump(rnds[-1]))
    end
  end

  def test_mt
    assert_random_int(%w(1067595299  955945823  477289528 4107218783 4228976476),
                      0x100000000, 0x00000456_00000345_00000234_00000123)
  end

  def test_0x3fffffff
    assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
                      0x3fffffff)
  end

  def test_0x40000000
    assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
                      0x40000000)
  end

  def test_0x40000001
    assert_random_int(%w(209652396 398764591 924231285 441365315 192771779),
                      0x40000001)
  end

  def test_0xffffffff
    assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
                      0xffffffff)
  end

  def test_0x100000000
    assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
                      0x100000000)
  end

  def test_0x100000001
    assert_random_int(%w(2546248239 1277901399 243580376 1171049868 2051556033),
                      0x100000001)
  end

  def test_rand_0x100000000
    assert_random_int(%w(4119812344 3870378946 80324654 4294967296 410016213),
                      0x100000001, 311702798)
  end

  def test_0x1000000000000
    assert_random_int(%w(11736396900911
                         183025067478208
                         197104029029115
                         130583529618791
                         180361239846611),
                      0x1000000000000)
  end

  def test_0x1000000000001
    assert_random_int(%w(187121911899765
                         197104029029115
                         180361239846611
                         236336749852452
                         208739549485656),
                      0x1000000000001)
  end

  def test_0x3fffffffffffffff
    assert_random_int(%w(900450186894289455
                         3969543146641149120
                         1895649597198586619
                         827948490035658087
                         3203365596207111891),
                      0x3fffffffffffffff)
  end

  def test_0x4000000000000000
    assert_random_int(%w(900450186894289455
                         3969543146641149120
                         1895649597198586619
                         827948490035658087
                         3203365596207111891),
                      0x4000000000000000)
  end

  def test_0x4000000000000001
    assert_random_int(%w(900450186894289455
                         3969543146641149120
                         1895649597198586619
                         827948490035658087
                         2279347887019741461),
                      0x4000000000000001)
  end

  def test_neg_0x10000000000
    ws = %w(455570294424 1073054410371 790795084744 2445173525 1088503892627)
    assert_random_int(ws, 0x10000000000, 3)
    assert_random_int(ws, -0x10000000000, 3)
  end

  def test_neg_0x10000
    ws = %w(2732 43567 42613 52416 45891)
    assert_random_int(ws, 0x10000)
    assert_random_int(ws, -0x10000)
  end

  def test_types
    srand(0)
    rnd = Random.new(0)
    assert_equal(44, rand(100.0))
    assert_equal(44, rnd.int(100.0))
    assert_equal(1245085576965981900420779258691, rand((2**100).to_f))
    assert_equal(1245085576965981900420779258691, rnd.int((2**100).to_f))
    assert_equal(914679880601515615685077935113, rand(-(2**100).to_f))
    assert_equal(914679880601515615685077935113, rnd.int(-(2**100).to_f))

    srand(0)
    rnd = Random.new(0)
    assert_equal(997707939797331598305742933184, rand(2**100))
    assert_equal(997707939797331598305742933184, rnd.int(2**100))
    assert_in_delta(0.602763376071644, rand((2**100).coerce(0).first),
                    0.000000000000001)
    assert_equal(0, rnd.int((2**100).coerce(0).first))

    srand(0)
    rnd = Random.new(0)
    assert_in_delta(0.548813503927325, rand(nil),
                    0.000000000000001)
    assert_in_delta(0.548813503927325, rnd.float(),
                    0.000000000000001)
    srand(0)
    rnd = Random.new(0)
    o = Object.new
    def o.to_int; 100; end
    assert_equal(44, rand(o))
    assert_equal(44, rnd.int(o))
    assert_equal(47, rand(o))
    assert_equal(47, rnd.int(o))
    assert_equal(64, rand(o))
    assert_equal(64, rnd.int(o))
  end

  def test_srand
    srand
    assert_kind_of(Integer, rand(2))
    assert_kind_of(Integer, Random.new.int(2))

    srand(2**100)
    rnd = Random.new(2**100)
    %w(3258412053).each {|w|
      assert_equal(w.to_i, rand(0x100000000))
      assert_equal(w.to_i, rnd.int(0x100000000))
    }
  end

  def test_shuffle
    srand(0)
    assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle)
  end

  def test_big_seed
    assert_random_int(%w(1143843490), 0x100000000, 2**1000000-1)
  end

  def test_random_gc
    r = Random.new(0)
    %w(2357136044 2546248239 3071714933).each do |w|
      assert_equal(w.to_i, r.int(0x100000000))
    end
    GC.start
    %w(3626093760 2588848963 3684848379).each do |w|
      assert_equal(w.to_i, r.int(0x100000000))
    end
  end

  def test_random_type_error
    assert_raise(TypeError) { Random.new(Object.new) }
    assert_raise(TypeError) { Random.new(0).int(Object.new) }
  end

  def test_random_argument_error
    assert_raise(ArgumentError) { Random.new(0).float(0, 0) }
  end

  def test_random_seed
    assert_equal(0, Random.new(0).seed)
    assert_equal(0x100000000, Random.new(0x100000000).seed)
    assert_equal(2**100, Random.new(2**100).seed)
  end

  def test_random_dup
    r1 = Random.new(0)
    r2 = r1.dup
    %w(2357136044 2546248239 3071714933).each do |w|
      assert_equal(w.to_i, r1.int(0x100000000))
    end
    %w(2357136044 2546248239 3071714933).each do |w|
      assert_equal(w.to_i, r2.int(0x100000000))
    end
    r2 = r1.dup
    %w(3626093760 2588848963 3684848379).each do |w|
      assert_equal(w.to_i, r1.int(0x100000000))
    end
    %w(3626093760 2588848963 3684848379).each do |w|
      assert_equal(w.to_i, r2.int(0x100000000))
    end
  end

  def test_random_state
    state = <<END
3877134065023083674777481835852171977222677629000095857864323111193832400974413
4782302161934463784850675209112299537259006497924090422596764895633625964527441
6943943249411681406395713106007661119327771293929504639878577616749110507385924
0173026285378896836022134086386136835407107422834685854738117043791709411958489
3504364936306163473541948635570644161010981140452515307286926529085424765299100
1255453260115310687580777474046203049197643434654645011966794531914127596390825
0832232869378617194193100828000236737535657699356156021286278281306055217995213
8911536025132779573429499813926910299964681785069915877910855089314686097947757
2621451199734871158015842198110309034467412292693435515184023707918034746119728
8223459645048255809852819129671833854560563104716892857257229121211527031509280
2390605053896565646658122125171846129817536096211475312518457776328637574563312
8113489216547503743508184872149896518488714209752552442327273883060730945969461
6568672445225657265545983966820639165285082194907591432296265618266901318398982
0560425129536975583916120558652408261759226803976460322062347123360444839683204
9868507788028894111577023917218846128348302845774997500569465902983227180328307
3735301552935104196244116381766459468172162284042207680945316590536094294865648
5953156978630954893391701383648157037914019502853776972615500142898763385846315
8457790690531675205213829055442306187692107777193071680668153335688203945049935
3404449910419303330872435985327845889440458370416464132629866593538629877042969
7589948685901343135964343582727302330074331803900821801076139161904900333497836
6627028345784450211920229170280462474456504317367706849373957309797251052447898
8436235456995644876515032202483449807136139063937892187299239634252391529822163
9187055268750679730919937006195967184206072757082920250075756273182004964087790
3812024063897424219316687828337007888030994779068081666133751070479581394604974
6022215489604777611774245181115126041390161592199230774968475944753915533936834
4740049163514318351045644344358598159463605453475585370226041981040238023241538
4958436364776113598408428801867643946791659645708540669432995503575075657406359
8086928867900590554805639837071298576728564946552163206007997000988745940681607
4542883814997403673656291618517107133421335645430345871041730410669209035640945
5024601618318371192091626092482640364669969307766919645222516407626038616667754
5781148898846306894862390724358039251444333889446128074209417936830253204064223
3424784857908022314095011879203259864909560830176189727132432100010493659154644
8407326292884826469503093409465946018496358514999175268200846200025235441426140
7783386235191526371372655894290440356560751680752191224383460972099834655086068
9989413443881686756951804138910911737670495391762470293978321414964443502180391
4665982575919524372985773336921990352313629822677022891307943536442258282401255
5387646898976193134193506239982621725093291970351083631367582570375381334759004
1784150668048523676387894646666460369896619585113435743180899362844070393586212
5023920017185866399742380706352739465848708746963693663004068892056705603018655
8686663087894205699555906146534549176352859823832196938386172810274748624517052
8356758650653040545267425513047130342286119889879774951060662713807133125543465
5104086026298674827575216701372513525846650644773241437066782037334367982012148
7987782004646896468089089826267467005660035604553432197455616078731159778086155
9443250946037119223468305483694093795324036812927501783593256716590840500905291
2096608538205270323065573109227029887731553399324547696295234105140157179430410
4003109602564833086703863221058381556776789018351726488797845637981974580864082
1630093543020854542240690897858757985640869209737744458407777584279553258261599
0246922348101147034463235613998979344685018577901996218099622190722307356620796
5137485271371502385527388080824050288371607602101805675021790116223360483508538
8832149997794718410946818866375912486788005950091851067237358294899771385995876
7088239104394332452501033090159333224995108984871426750597513314521294001864578
2353528356752869732412552685554334966798888534847483030947310518891788722418172
6008607577773612004956373863580996793809969715725508939568919714424871639667201
7922255031431159347210833846575355772055570279673262115911154370983086189948124
4653677615895887099814174914248255026619941911735341818489822197472499295786997
7728418516719104857455960900092226749725407204388193002835497055305427730656889
1508308778869166073740855838213112709306743479676740893150000714099064468263284
1873435518542972182497755500300784177067568586395485329021157235696300013490087
2866571034916258390528533374944905429089028336079264760836949419754851422499614
5732326011260304142074554782259843903215064144396140106592193961703288125005023
5334375212799817540775536847622032852415253966587517800661605905489339306359573
2234947905196298436841723673626428243649931398749552780311877734063703985375067
1239508613417041942487245370152912391885566432830659640677893488723724763120121
4111855277511356759926232894062814360449757490961653026194107761340614059045172
1123363102660719217740126157997033682099769790976313166682432732518101889210276
9574144065390305904944821051736021310524344626348851573631697771556587859836330
6997324121866564283654784470215100159122764509197570402997911258816526554863326
9877535269005418736225944874608987238997316999444215865249840762640949599725696
0773083894168959823152054508672272612355108904098579447774398451678239199426513
3439507737424049578587487505080347686371029156845461151278198605267053408259090
3158676794894709281917034995611352710898103415304769654883981727681820369090169
9295163908214854813365413456264812190842699054830709079275249714169405719140093
1347572458245530016346604698682269779841803667099480215265926316505737171177810
9969036572310084022695109125200937135540995157279354438704321290061646592229860
0156566013602344870223183295508278359111174872740360473845615437106413256386849
2286259982118315248148847764929974917157683083659364623458927512616369119194574
2254080
END
    state = state.split.join.to_i
    r = Random.new(0)
    srand(0)
    assert_equal(state, r.instance_eval { state })
    assert_equal(state, Random.instance_eval { state })
    r.int(0x100)
    assert_equal(state, r.instance_eval { state })
  end

  def test_random_left
    r = Random.new(0)
    assert_equal(1, r.instance_eval { left })
    r.int(0x100)
    assert_equal(624, r.instance_eval { left })
    r.int(0x100)
    assert_equal(623, r.instance_eval { left })
    srand(0)
    assert_equal(1, Random.instance_eval { left })
    rand(0x100)
    assert_equal(624, Random.instance_eval { left })
    rand(0x100)
    assert_equal(623, Random.instance_eval { left })
  end

  def test_random_bytes
    r = Random.new(0)
    assert_equal("", r.bytes(0))
    assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1))
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),                                                                                                                              r.bytes(10))
  end

  def test_random_range
    r = Random.new(0)
    %w(9 5 8).each {|w| assert_equal(w.to_i, r.int(5..9)) }
    %w(-237 731 383).each {|w| assert_equal(w.to_i, r.int(-1000..1000)) }
    %w(1267650600228229401496703205382
       1267650600228229401496703205384
       1267650600228229401496703205383).each do |w|
      assert_equal(w.to_i, r.int(2**100+5..2**100+9))
    end
  end

  def test_random_float
    r = Random.new(0)
    assert_in_delta(0.5488135039273248, r.float, 0.0001)
    assert_in_delta(0.7151893663724195, r.float, 0.0001)
    assert_in_delta(0.6027633760716439, r.float, 0.0001)
    assert_in_delta(1.0897663659937937, r.float(2), 0.0001)
    assert_in_delta(5.3704626067153264e+29, r.float(2**100), 10**25)

    # is this intentional?
    assert_raise(TypeError) { r.float(1..2) }
  end

  def test_random_equal
    r = Random.new(0)
    assert(r == r)
    assert(r == r.dup)
    r1 = r.dup
    r2 = r.dup
    r1.int(0x100)
    assert(r1 != r2)
    r2.int(0x100)
    assert(r1 == r2)
  end
end