Mercurial > vim
comparison src/testdir/test_random.vim @ 18703:aef98a646d3e v8.1.2343
patch 8.1.2343: using time() for srand() is not very random
Commit: https://github.com/vim/vim/commit/07e4a197953d12902fb97beb48830a5323a52280
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Nov 26 12:23:30 2019 +0100
patch 8.1.2343: using time() for srand() is not very random
Problem: Using time() for srand() is not very random.
Solution: use /dev/urandom if available
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 26 Nov 2019 12:30:04 +0100 |
parents | 128662297ddf |
children | 2513e666aa82 |
comparison
equal
deleted
inserted
replaced
18702:fd7bbf832d8f | 18703:aef98a646d3e |
---|---|
9 call assert_equal(3633119408, rand(r)) | 9 call assert_equal(3633119408, rand(r)) |
10 call assert_equal(516391518, rand(r)) | 10 call assert_equal(516391518, rand(r)) |
11 | 11 |
12 call test_settime(12341234) | 12 call test_settime(12341234) |
13 let s = srand() | 13 let s = srand() |
14 call assert_equal(s, srand()) | 14 if filereadable('/dev/urandom') |
15 call test_settime(12341235) | 15 " using /dev/urandom |
16 call assert_notequal(s, srand()) | 16 call assert_notequal(s, srand()) |
17 else | |
18 " using time() | |
19 call assert_equal(s, srand()) | |
20 call test_settime(12341235) | |
21 call assert_notequal(s, srand()) | |
22 endif | |
17 | 23 |
18 call srand() | 24 call srand() |
19 let v = rand() | 25 let v = rand() |
20 call assert_notequal(v, rand()) | 26 call assert_notequal(v, rand()) |
21 | 27 |
23 call assert_fails('echo rand([1, 2, 3])', 'E475:') | 29 call assert_fails('echo rand([1, 2, 3])', 'E475:') |
24 call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:') | 30 call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:') |
25 call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') | 31 call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') |
26 call assert_fails('echo rand([1, 2, [3], 4])', 'E475:') | 32 call assert_fails('echo rand([1, 2, [3], 4])', 'E475:') |
27 call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:') | 33 call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:') |
34 | |
35 call test_settime(0) | |
28 endfunc | 36 endfunc |