forked from vim-jp/vital.vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.txt
204 lines (154 loc) · 5.59 KB
/
Random.txt
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
*vital/Random.txt* Random utility frontend library
Maintainer: thinca <thinca+vim@gmail.com>
==============================================================================
CONTENTS *Vital.Random-contents*
INTRODUCTION |Vital.Random-introduction|
INTERFACE |Vital.Random-interface|
FUNCTIONS |Vital.Random-functions|
OBJECTS |Vital.Random-objects|
Random Object |Vital.Random-Random|
Generator Object |Vital.Random-Generator|
==============================================================================
INTRODUCTION *Vital.Random-introduction*
*Vital.Random* is a Random utility frontend library.
*Vital.Random-synopsis*
>
let s:V = vital#{plugin-name}#new()
let s:Random = s:V.import('Random')
" Generate a random number.
echo s:Random.next()
" => 854122453
" Generate 1 or more and 6 or less random number.
echo s:Random.range(1, 7)
" => 1
" Get a sample value from a list.
echo s:Random.sample(range(10))
" => 8
" Shuffle a list.
echo s:Random.shuffle(range(10))
" => [2, 3, 0, 9, 1, 6, 7, 5, 8, 4]
" Make a new Random Object.
let s:R = s:Random.new()
" Make a new Random Object with Generator.
let s:R = s:Random.new('Xor128')
" Make a new Random Object with seed.
let s:seed = s:Random.next()
let s:R = s:Random.new('', s:seed)
echo s:R.next(3)
" => [-962538367, 815574453, 655578969]
" Reset the seed.
call s:R.seed(s:seed)
echo s:R.next(3)
" => [-962538367, 815574453, 655578969]
==============================================================================
INTERFACE *Vital.Random-interface*
------------------------------------------------------------------------------
FUNCTIONS *Vital.Random-functions*
new([{generator} [, {seed}]]) *Vital.Random.new()*
Create a new Random Object(|Vital.Random-Random|).
{generator} is a generator name or a generator object.
A name of a generator is a portion of the last of a module name like
"Random.*". For example, "Xor128".
See |Vital.Random-Generator| about a generator object.
If {seed} is omitted, this is created from |Vital.Random.next()|.
make_seed() *Vital.Random.make_seed()*
Make a seed from current time and so on.
This does not assume being used continuously.
This is used to initialize the internal common random generator.
You can use |Vital.Random.next()| to initialize your Random Object.
next() *Vital.Random.next()*
next({n})
Shortcut to |Vital.Random-Random.next()|.
seed({seeds}) *Vital.Random.seed()*
Shortcut to |Vital.Random-Random.seed()|.
generate_canonical() *Vital.Random.generate_canonical()*
Shortcut to |Vital.Random-Random.generate_canonical()|.
range({to}) *Vital.Random.range()*
range({from}, {to})
Shortcut to |Vital.Random-Random.range()|.
bool() *Vital.Random.bool()*
Shortcut to |Vital.Random-Random.bool()|.
sample({list}) *Vital.Random.sample()*
sample({list}, {n})
Shortcut to |Vital.Random-Random.sample()|.
shuffle({list}) *Vital.Random.shuffle()*
Shortcut to |Vital.Random-Random.shuffle()|.
==============================================================================
OBJECTS *Vital.Random-objects*
------------------------------------------------------------------------------
Random Object *Vital.Random-Random*
A Random Object generates some useful random numbers from a generator.
You can choose the generator to be used freely.
Random.next() *Vital.Random-Random.next()*
Random.next({n})
Get a next random number or {n} next random numbers in the sequence.
Random.seed({seeds}) *Vital.Random-Random.seed()*
Set seeds by a |Number| or a |List| of numbers.
>
" Create two objects which generate the same random number sequence.
let s:player1 = s:Random.new()
let s:player2 = s:Random.new()
let s:seed = s:Random.next()
call s:player1.seed(s:seed)
call s:player2.seed(s:seed)
Random.generate_canonical() *Vital.Random-Random.generate_canonical()*
Get a random float number in [0.0, 1.0).
>
echo s:R.generate_canonical()
" 0.618474
echo s:R.generate_canonical()
" 0.350191
Random.range({to}) *Vital.Random-Random.range()*
Get a random number in [0, {to}).
If {to} is a |Number|, returns a |Number|.
If {to} is a |Float|, returns a |Float|.
>
echo s:R.range(5)
" 4
echo s:R.range(5)
" 0
Random.range({from}, {to})
Get a random number in [{from}, {to}).
If {from} and {to} is a |Number|, returns a |Number|.
If {from} or {to} is a |Float|, returns a |Float|.
>
echo s:R.range(1, 6)
" 1
echo s:R.range(1, 6)
" 3
Random.bool() *Vital.Random-Random.bool()*
Get a random number in 0 or 1.
>
echo s:R.bool()
" 1
echo s:R.bool()
" 0
Random.sample({list}) *Vital.Random-Random.sample()*
Random.sample({list}, {n})
Choose a random element or {n} random elements from the {list}.
>
echo s:R.sample(range(5))
" 1
echo s:R.sample(range(5), 3)
" [3, 1, 4]
Random.shuffle({list}) *Vital.Random-Random.shuffle()*
Shuffle the {list}.
>
let s:list = range(5)
echo s:R.shuffle(s:list)
" [3, 1, 0, 2, 4]
echo s:list
" [3, 1, 0, 2, 4]
------------------------------------------------------------------------------
Generator Object *Vital.Random-Generator*
A Generator Object generates random number sequence.
Generator.next() *Vital.Random-Generator.next()*
Get the next random number in the sequence.
Generator.min() *Vital.Random-Generator.min()*
Get the smallest value in the output range.
Generator.max() *Vital.Random-Generator.max()*
Get the largest value in the output range.
Generator.seed({seeds}) *Vital.Random-Generator.seed()*
Set seeds by a |List| of numbers which initializes the generator.
==============================================================================
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl