PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ruby/test_rand.rb

http://github.com/ruby/ruby
Ruby | 594 lines | 529 code | 63 blank | 2 comment | 2 complexity | 7f6e1abe6083d32212c224e2c667d303 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class TestRand < Test::Unit::TestCase
  4. def assert_random_int(ws, m, init = 0)
  5. srand(init)
  6. rnds = [Random.new(init)]
  7. rnds2 = [rnds[0].dup]
  8. rnds3 = [rnds[0].dup]
  9. ws.each_with_index do |w, i|
  10. w = w.to_i
  11. assert_equal(w, rand(m))
  12. rnds.each do |rnd|
  13. assert_equal(w, rnd.rand(m))
  14. end
  15. rnds2.each do |rnd|
  16. r=rnd.rand(i...(m+i))
  17. assert_equal(w+i, r)
  18. end
  19. rnds3.each do |rnd|
  20. r=rnd.rand(i..(m+i-1))
  21. assert_equal(w+i, r)
  22. end
  23. rnds << Marshal.load(Marshal.dump(rnds[-1]))
  24. rnds2 << Marshal.load(Marshal.dump(rnds2[-1]))
  25. end
  26. end
  27. def test_mt
  28. assert_random_int(%w(1067595299 955945823 477289528 4107218783 4228976476),
  29. 0x100000000, 0x00000456_00000345_00000234_00000123)
  30. end
  31. def test_0x3fffffff
  32. assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
  33. 0x3fffffff)
  34. end
  35. def test_0x40000000
  36. assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
  37. 0x40000000)
  38. end
  39. def test_0x40000001
  40. assert_random_int(%w(209652396 398764591 924231285 441365315 192771779),
  41. 0x40000001)
  42. end
  43. def test_0xffffffff
  44. assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
  45. 0xffffffff)
  46. end
  47. def test_0x100000000
  48. assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
  49. 0x100000000)
  50. end
  51. def test_0x100000001
  52. assert_random_int(%w(2546248239 1277901399 243580376 1171049868 2051556033),
  53. 0x100000001)
  54. end
  55. def test_rand_0x100000000
  56. assert_random_int(%w(4119812344 3870378946 80324654 4294967296 410016213),
  57. 0x100000001, 311702798)
  58. end
  59. def test_0x1000000000000
  60. assert_random_int(%w(11736396900911
  61. 183025067478208
  62. 197104029029115
  63. 130583529618791
  64. 180361239846611),
  65. 0x1000000000000)
  66. end
  67. def test_0x1000000000001
  68. assert_random_int(%w(187121911899765
  69. 197104029029115
  70. 180361239846611
  71. 236336749852452
  72. 208739549485656),
  73. 0x1000000000001)
  74. end
  75. def test_0x3fffffffffffffff
  76. assert_random_int(%w(900450186894289455
  77. 3969543146641149120
  78. 1895649597198586619
  79. 827948490035658087
  80. 3203365596207111891),
  81. 0x3fffffffffffffff)
  82. end
  83. def test_0x4000000000000000
  84. assert_random_int(%w(900450186894289455
  85. 3969543146641149120
  86. 1895649597198586619
  87. 827948490035658087
  88. 3203365596207111891),
  89. 0x4000000000000000)
  90. end
  91. def test_0x4000000000000001
  92. assert_random_int(%w(900450186894289455
  93. 3969543146641149120
  94. 1895649597198586619
  95. 827948490035658087
  96. 2279347887019741461),
  97. 0x4000000000000001)
  98. end
  99. def test_0x10000000000
  100. ws = %w(455570294424 1073054410371 790795084744 2445173525 1088503892627)
  101. assert_random_int(ws, 0x10000000000, 3)
  102. end
  103. def test_0x10000
  104. ws = %w(2732 43567 42613 52416 45891)
  105. assert_random_int(ws, 0x10000)
  106. end
  107. def test_types
  108. srand(0)
  109. rnd = Random.new(0)
  110. assert_equal(44, rand(100.0))
  111. assert_equal(44, rnd.rand(100))
  112. assert_equal(1245085576965981900420779258691, rand((2**100).to_f))
  113. assert_equal(1245085576965981900420779258691, rnd.rand(2**100))
  114. assert_equal(914679880601515615685077935113, rand(-(2**100).to_f))
  115. srand(0)
  116. rnd = Random.new(0)
  117. assert_equal(997707939797331598305742933184, rand(2**100))
  118. assert_equal(997707939797331598305742933184, rnd.rand(2**100))
  119. assert_in_delta(0.602763376071644, rand((2**100).coerce(0).first),
  120. 0.000000000000001)
  121. assert_raise(ArgumentError) {rnd.rand((2**100).coerce(0).first)}
  122. srand(0)
  123. rnd = Random.new(0)
  124. assert_in_delta(0.548813503927325, rand(nil),
  125. 0.000000000000001)
  126. assert_in_delta(0.548813503927325, rnd.rand(),
  127. 0.000000000000001)
  128. srand(0)
  129. rnd = Random.new(0)
  130. o = Object.new
  131. def o.to_int; 100; end
  132. assert_equal(44, rand(o))
  133. assert_equal(44, rnd.rand(o))
  134. assert_equal(47, rand(o))
  135. assert_equal(47, rnd.rand(o))
  136. assert_equal(64, rand(o))
  137. assert_equal(64, rnd.rand(o))
  138. end
  139. def test_srand
  140. srand
  141. assert_kind_of(Integer, rand(2))
  142. assert_kind_of(Integer, Random.new.rand(2))
  143. srand(2**100)
  144. rnd = Random.new(2**100)
  145. %w(3258412053).each {|w|
  146. assert_equal(w.to_i, rand(0x100000000))
  147. assert_equal(w.to_i, rnd.rand(0x100000000))
  148. }
  149. end
  150. def test_shuffle
  151. srand(0)
  152. result = [*1..5].shuffle
  153. assert_equal([*1..5], result.sort)
  154. assert_equal(result, [*1..5].shuffle(random: Random.new(0)))
  155. end
  156. def test_big_seed
  157. assert_random_int(%w(2757555016), 0x100000000, 2**1000000-1)
  158. end
  159. def test_random_gc
  160. r = Random.new(0)
  161. %w(2357136044 2546248239 3071714933).each do |w|
  162. assert_equal(w.to_i, r.rand(0x100000000))
  163. end
  164. GC.start
  165. %w(3626093760 2588848963 3684848379).each do |w|
  166. assert_equal(w.to_i, r.rand(0x100000000))
  167. end
  168. end
  169. def test_random_type_error
  170. assert_raise(TypeError) { Random.new(Object.new) }
  171. assert_raise(TypeError) { Random.new(0).rand(Object.new) }
  172. end
  173. def test_random_argument_error
  174. r = Random.new(0)
  175. assert_raise(ArgumentError) { r.rand(0, 0) }
  176. assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1) }
  177. assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1.0) }
  178. assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(0) }
  179. assert_equal(0, r.rand(1), '[ruby-dev:39166]')
  180. assert_equal(0, r.rand(0...1), '[ruby-dev:39166]')
  181. assert_equal(0, r.rand(0..0), '[ruby-dev:39166]')
  182. assert_equal(0.0, r.rand(0.0..0.0), '[ruby-dev:39166]')
  183. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0...0) }
  184. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0..-1) }
  185. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...0.0) }
  186. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...-0.1) }
  187. bug3027 = '[ruby-core:29075]'
  188. assert_raise(ArgumentError, bug3027) { r.rand(nil) }
  189. end
  190. def test_random_seed
  191. assert_equal(0, Random.new(0).seed)
  192. assert_equal(0x100000000, Random.new(0x100000000).seed)
  193. assert_equal(2**100, Random.new(2**100).seed)
  194. end
  195. def test_random_dup
  196. r1 = Random.new(0)
  197. r2 = r1.dup
  198. %w(2357136044 2546248239 3071714933).each do |w|
  199. assert_equal(w.to_i, r1.rand(0x100000000))
  200. end
  201. %w(2357136044 2546248239 3071714933).each do |w|
  202. assert_equal(w.to_i, r2.rand(0x100000000))
  203. end
  204. r2 = r1.dup
  205. %w(3626093760 2588848963 3684848379).each do |w|
  206. assert_equal(w.to_i, r1.rand(0x100000000))
  207. end
  208. %w(3626093760 2588848963 3684848379).each do |w|
  209. assert_equal(w.to_i, r2.rand(0x100000000))
  210. end
  211. end
  212. def test_random_state
  213. state = <<END
  214. 3877134065023083674777481835852171977222677629000095857864323111193832400974413
  215. 4782302161934463784850675209112299537259006497924090422596764895633625964527441
  216. 6943943249411681406395713106007661119327771293929504639878577616749110507385924
  217. 0173026285378896836022134086386136835407107422834685854738117043791709411958489
  218. 3504364936306163473541948635570644161010981140452515307286926529085424765299100
  219. 1255453260115310687580777474046203049197643434654645011966794531914127596390825
  220. 0832232869378617194193100828000236737535657699356156021286278281306055217995213
  221. 8911536025132779573429499813926910299964681785069915877910855089314686097947757
  222. 2621451199734871158015842198110309034467412292693435515184023707918034746119728
  223. 8223459645048255809852819129671833854560563104716892857257229121211527031509280
  224. 2390605053896565646658122125171846129817536096211475312518457776328637574563312
  225. 8113489216547503743508184872149896518488714209752552442327273883060730945969461
  226. 6568672445225657265545983966820639165285082194907591432296265618266901318398982
  227. 0560425129536975583916120558652408261759226803976460322062347123360444839683204
  228. 9868507788028894111577023917218846128348302845774997500569465902983227180328307
  229. 3735301552935104196244116381766459468172162284042207680945316590536094294865648
  230. 5953156978630954893391701383648157037914019502853776972615500142898763385846315
  231. 8457790690531675205213829055442306187692107777193071680668153335688203945049935
  232. 3404449910419303330872435985327845889440458370416464132629866593538629877042969
  233. 7589948685901343135964343582727302330074331803900821801076139161904900333497836
  234. 6627028345784450211920229170280462474456504317367706849373957309797251052447898
  235. 8436235456995644876515032202483449807136139063937892187299239634252391529822163
  236. 9187055268750679730919937006195967184206072757082920250075756273182004964087790
  237. 3812024063897424219316687828337007888030994779068081666133751070479581394604974
  238. 6022215489604777611774245181115126041390161592199230774968475944753915533936834
  239. 4740049163514318351045644344358598159463605453475585370226041981040238023241538
  240. 4958436364776113598408428801867643946791659645708540669432995503575075657406359
  241. 8086928867900590554805639837071298576728564946552163206007997000988745940681607
  242. 4542883814997403673656291618517107133421335645430345871041730410669209035640945
  243. 5024601618318371192091626092482640364669969307766919645222516407626038616667754
  244. 5781148898846306894862390724358039251444333889446128074209417936830253204064223
  245. 3424784857908022314095011879203259864909560830176189727132432100010493659154644
  246. 8407326292884826469503093409465946018496358514999175268200846200025235441426140
  247. 7783386235191526371372655894290440356560751680752191224383460972099834655086068
  248. 9989413443881686756951804138910911737670495391762470293978321414964443502180391
  249. 4665982575919524372985773336921990352313629822677022891307943536442258282401255
  250. 5387646898976193134193506239982621725093291970351083631367582570375381334759004
  251. 1784150668048523676387894646666460369896619585113435743180899362844070393586212
  252. 5023920017185866399742380706352739465848708746963693663004068892056705603018655
  253. 8686663087894205699555906146534549176352859823832196938386172810274748624517052
  254. 8356758650653040545267425513047130342286119889879774951060662713807133125543465
  255. 5104086026298674827575216701372513525846650644773241437066782037334367982012148
  256. 7987782004646896468089089826267467005660035604553432197455616078731159778086155
  257. 9443250946037119223468305483694093795324036812927501783593256716590840500905291
  258. 2096608538205270323065573109227029887731553399324547696295234105140157179430410
  259. 4003109602564833086703863221058381556776789018351726488797845637981974580864082
  260. 1630093543020854542240690897858757985640869209737744458407777584279553258261599
  261. 0246922348101147034463235613998979344685018577901996218099622190722307356620796
  262. 5137485271371502385527388080824050288371607602101805675021790116223360483508538
  263. 8832149997794718410946818866375912486788005950091851067237358294899771385995876
  264. 7088239104394332452501033090159333224995108984871426750597513314521294001864578
  265. 2353528356752869732412552685554334966798888534847483030947310518891788722418172
  266. 6008607577773612004956373863580996793809969715725508939568919714424871639667201
  267. 7922255031431159347210833846575355772055570279673262115911154370983086189948124
  268. 4653677615895887099814174914248255026619941911735341818489822197472499295786997
  269. 7728418516719104857455960900092226749725407204388193002835497055305427730656889
  270. 1508308778869166073740855838213112709306743479676740893150000714099064468263284
  271. 1873435518542972182497755500300784177067568586395485329021157235696300013490087
  272. 2866571034916258390528533374944905429089028336079264760836949419754851422499614
  273. 5732326011260304142074554782259843903215064144396140106592193961703288125005023
  274. 5334375212799817540775536847622032852415253966587517800661605905489339306359573
  275. 2234947905196298436841723673626428243649931398749552780311877734063703985375067
  276. 1239508613417041942487245370152912391885566432830659640677893488723724763120121
  277. 4111855277511356759926232894062814360449757490961653026194107761340614059045172
  278. 1123363102660719217740126157997033682099769790976313166682432732518101889210276
  279. 9574144065390305904944821051736021310524344626348851573631697771556587859836330
  280. 6997324121866564283654784470215100159122764509197570402997911258816526554863326
  281. 9877535269005418736225944874608987238997316999444215865249840762640949599725696
  282. 0773083894168959823152054508672272612355108904098579447774398451678239199426513
  283. 3439507737424049578587487505080347686371029156845461151278198605267053408259090
  284. 3158676794894709281917034995611352710898103415304769654883981727681820369090169
  285. 9295163908214854813365413456264812190842699054830709079275249714169405719140093
  286. 1347572458245530016346604698682269779841803667099480215265926316505737171177810
  287. 9969036572310084022695109125200937135540995157279354438704321290061646592229860
  288. 0156566013602344870223183295508278359111174872740360473845615437106413256386849
  289. 2286259982118315248148847764929974917157683083659364623458927512616369119194574
  290. 2254080
  291. END
  292. state = state.split.join.to_i
  293. r = Random.new(0)
  294. srand(0)
  295. assert_equal(state, r.instance_eval { state })
  296. assert_equal(state, Random.instance_eval { state })
  297. r.rand(0x100)
  298. assert_equal(state, r.instance_eval { state })
  299. end
  300. def test_random_left
  301. r = Random.new(0)
  302. assert_equal(1, r.instance_eval { left })
  303. r.rand(0x100)
  304. assert_equal(624, r.instance_eval { left })
  305. r.rand(0x100)
  306. assert_equal(623, r.instance_eval { left })
  307. srand(0)
  308. assert_equal(1, Random.instance_eval { left })
  309. rand(0x100)
  310. assert_equal(624, Random.instance_eval { left })
  311. rand(0x100)
  312. assert_equal(623, Random.instance_eval { left })
  313. end
  314. def test_random_bytes
  315. assert_random_bytes(Random.new(0))
  316. end
  317. def assert_random_bytes(r)
  318. srand(0)
  319. assert_equal("", r.bytes(0))
  320. assert_equal("", Random.bytes(0))
  321. x = "\xAC".force_encoding("ASCII-8BIT")
  322. assert_equal(x, r.bytes(1))
  323. assert_equal(x, Random.bytes(1))
  324. x = "/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT")
  325. assert_equal(x, r.bytes(10))
  326. assert_equal(x, Random.bytes(10))
  327. end
  328. def test_random_range
  329. srand(0)
  330. r = Random.new(0)
  331. %w(9 5 8).each {|w|
  332. assert_equal(w.to_i, rand(5..9))
  333. assert_equal(w.to_i, r.rand(5..9))
  334. }
  335. %w(-237 731 383).each {|w|
  336. assert_equal(w.to_i, rand(-1000..1000))
  337. assert_equal(w.to_i, r.rand(-1000..1000))
  338. }
  339. %w(1267650600228229401496703205382
  340. 1267650600228229401496703205384
  341. 1267650600228229401496703205383).each do |w|
  342. assert_equal(w.to_i, rand(2**100+5..2**100+9))
  343. assert_equal(w.to_i, r.rand(2**100+5..2**100+9))
  344. end
  345. v = rand(3.1..4)
  346. assert_instance_of(Float, v, '[ruby-core:24679]')
  347. assert_include(3.1..4, v)
  348. v = r.rand(3.1..4)
  349. assert_instance_of(Float, v, '[ruby-core:24679]')
  350. assert_include(3.1..4, v)
  351. now = Time.now
  352. assert_equal(now, rand(now..now))
  353. assert_equal(now, r.rand(now..now))
  354. end
  355. def test_random_float
  356. r = Random.new(0)
  357. assert_in_delta(0.5488135039273248, r.rand, 0.0001)
  358. assert_in_delta(0.7151893663724195, r.rand, 0.0001)
  359. assert_in_delta(0.6027633760716439, r.rand, 0.0001)
  360. assert_in_delta(1.0897663659937937, r.rand(2.0), 0.0001)
  361. assert_in_delta(5.3704626067153264e+29, r.rand((2**100).to_f), 10**25)
  362. assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(1.0 / 0.0) }
  363. assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(0.0 / 0.0) }
  364. assert_raise(Errno::EDOM) {r.rand(1..)}
  365. assert_raise(Errno::EDOM) {r.rand(..1)}
  366. r = Random.new(0)
  367. assert_in_delta(1.5488135039273248, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
  368. assert_in_delta(1.7151893663724195, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
  369. assert_in_delta(7.027633760716439, r.rand(1.0...11.0), 0.0001, '[ruby-core:24655]')
  370. assert_in_delta(3.0897663659937937, r.rand(2.0...4.0), 0.0001, '[ruby-core:24655]')
  371. assert_nothing_raised {r.rand(-Float::MAX..Float::MAX)}
  372. end
  373. def test_random_equal
  374. r = Random.new(0)
  375. assert_equal(r, r)
  376. assert_equal(r, r.dup)
  377. r1 = r.dup
  378. r2 = r.dup
  379. r1.rand(0x100)
  380. assert_not_equal(r1, r2)
  381. r2.rand(0x100)
  382. assert_equal(r1, r2)
  383. end
  384. def test_fork_shuffle
  385. pid = fork do
  386. (1..10).to_a.shuffle
  387. raise 'default seed is not set' if srand == 0
  388. end
  389. _, st = Process.waitpid2(pid)
  390. assert_predicate(st, :success?, "#{st.inspect}")
  391. rescue NotImplementedError, ArgumentError
  392. end
  393. def assert_fork_status(n, mesg, &block)
  394. IO.pipe do |r, w|
  395. (1..n).map do
  396. st = desc = nil
  397. IO.pipe do |re, we|
  398. p1 = fork {
  399. re.close
  400. STDERR.reopen(we)
  401. w.puts(block.call.to_s)
  402. }
  403. we.close
  404. err = Thread.start {re.read}
  405. _, st = Process.waitpid2(p1)
  406. desc = FailDesc[st, mesg, err.value]
  407. end
  408. assert(!st.signaled?, desc)
  409. assert(st.success?, mesg)
  410. r.gets.strip
  411. end
  412. end
  413. end
  414. def test_rand_reseed_on_fork
  415. GC.start
  416. bug5661 = '[ruby-core:41209]'
  417. assert_fork_status(1, bug5661) {Random.rand(4)}
  418. r1, r2 = *assert_fork_status(2, bug5661) {Random.rand}
  419. assert_not_equal(r1, r2, bug5661)
  420. assert_fork_status(1, bug5661) {rand(4)}
  421. r1, r2 = *assert_fork_status(2, bug5661) {rand}
  422. assert_not_equal(r1, r2, bug5661)
  423. stable = Random.new
  424. assert_fork_status(1, bug5661) {stable.rand(4)}
  425. r1, r2 = *assert_fork_status(2, bug5661) {stable.rand}
  426. assert_equal(r1, r2, bug5661)
  427. assert_fork_status(1, '[ruby-core:82100] [Bug #13753]') do
  428. Random::DEFAULT.rand(4)
  429. end
  430. rescue NotImplementedError
  431. end
  432. def test_seed
  433. bug3104 = '[ruby-core:29292]'
  434. rand_1 = Random.new(-1).rand
  435. assert_not_equal(rand_1, Random.new((1 << 31) -1).rand, "#{bug3104} (2)")
  436. assert_not_equal(rand_1, Random.new((1 << 63) -1).rand, "#{bug3104} (2)")
  437. [-1, -2**10, -2**40].each {|n|
  438. b = (2**64).coerce(n)[0]
  439. r1 = Random.new(n).rand
  440. r2 = Random.new(b).rand
  441. assert_equal(r1, r2)
  442. }
  443. end
  444. def test_default
  445. r1 = Random::DEFAULT.dup
  446. r2 = Random::DEFAULT.dup
  447. 3.times do
  448. x0 = rand
  449. x1 = r1.rand
  450. x2 = r2.rand
  451. assert_equal(x0, x1)
  452. assert_equal(x0, x2)
  453. end
  454. end
  455. def test_marshal
  456. bug3656 = '[ruby-core:31622]'
  457. assert_raise(TypeError, bug3656) {
  458. Random.new.__send__(:marshal_load, 0)
  459. }
  460. end
  461. def test_initialize_frozen
  462. r = Random.new(0)
  463. r.freeze
  464. assert_raise(FrozenError, '[Bug #6540]') do
  465. r.__send__(:initialize, r)
  466. end
  467. end
  468. def test_marshal_load_frozen
  469. r = Random.new(0)
  470. d = r.__send__(:marshal_dump)
  471. r.freeze
  472. assert_raise(FrozenError, '[Bug #6540]') do
  473. r.__send__(:marshal_load, d)
  474. end
  475. end
  476. def test_random_ulong_limited
  477. def (gen = Object.new).rand(*) 1 end
  478. assert_equal([2], (1..100).map {[1,2,3].sample(random: gen)}.uniq)
  479. def (gen = Object.new).rand(*) 100 end
  480. assert_raise_with_message(RangeError, /big 100\z/) {[1,2,3].sample(random: gen)}
  481. bug7903 = '[ruby-dev:47061] [Bug #7903]'
  482. def (gen = Object.new).rand(*) -1 end
  483. assert_raise_with_message(RangeError, /small -1\z/, bug7903) {[1,2,3].sample(random: gen)}
  484. bug7935 = '[ruby-core:52779] [Bug #7935]'
  485. class << (gen = Object.new)
  486. def rand(limit) @limit = limit; 0 end
  487. attr_reader :limit
  488. end
  489. [1, 2].sample(1, random: gen)
  490. assert_equal(2, gen.limit, bug7935)
  491. end
  492. def test_random_ulong_limited_no_rand
  493. c = Class.new do
  494. undef rand
  495. def bytes(n)
  496. "\0"*n
  497. end
  498. end
  499. gen = c.new.extend(Random::Formatter)
  500. assert_equal(1, [1, 2].sample(random: gen))
  501. end
  502. def test_default_seed
  503. assert_separately([], <<-End)
  504. seed = Random::DEFAULT::seed
  505. rand1 = Random::DEFAULT::rand
  506. rand2 = Random.new(seed).rand
  507. assert_equal(rand1, rand2)
  508. srand seed
  509. rand3 = rand
  510. assert_equal(rand1, rand3)
  511. End
  512. end
  513. def test_urandom
  514. [0, 1, 100].each do |size|
  515. v = Random.urandom(size)
  516. assert_kind_of(String, v)
  517. assert_equal(size, v.bytesize)
  518. end
  519. end
  520. def test_new_seed
  521. size = 0
  522. n = 8
  523. n.times do
  524. v = Random.new_seed
  525. assert_kind_of(Integer, v)
  526. size += v.size
  527. end
  528. # probability of failure <= 1/256**8
  529. assert_operator(size.fdiv(n), :>, 15)
  530. end
  531. end