generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.go
688 lines (546 loc) · 17.8 KB
/
number.go
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package random
import "math"
// Int returns a random integer between min and max, inclusive.
// If min > max, Int will panic.
func Int(min, max int) int {
if min > max {
panic("Random int minimum cannot be greater than maximum")
}
// Handle the edge case where min == 0 and max == math.MaxInt.
if min == 0 && max == math.MaxInt {
return r.Int()
}
return r.Intn(max-min+1) + min
}
// IntSlice returns a slice of random integers between min and max, inclusive.
// If min > max, IntSlice will panic.
// If count is less or euqal to 0, IntSliceUnique will return an empty slice.
func IntSlice(min, max, count int) []int {
if count <= 0 {
return []int{}
}
s := make([]int, count)
for i := range s {
s[i] = Int(min, max)
}
return s
}
// IntSliceUnique returns a slice of unique random integers between min and max, inclusive.
// If min > max, IntSliceUnique will panic.
// If count is equal or less than 0, IntSliceUnique will return an empty slice.
// If count is greater than the number of integers between min and max, IntSliceUnique will panic.
func IntSliceUnique(min, max, count int) []int {
if count <= 0 {
return []int{}
}
if count > max-min+1 {
panic("Random int slice unique count cannot be greater than the number of integers between min and max")
}
s, used := make([]int, count), make(map[int]bool, count)
for i := 0; i < count; i++ {
randInt := Int(min, max)
for used[randInt] {
randInt = Int(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Int8 returns a random int8 between min and max, inclusive.
// If min > max, Int8 panics.
func Int8(min, max int8) int8 {
if min > max {
panic("Random int8 minimum cannot be greater than maximum")
}
return int8(Int32(int32(min), int32(max)))
}
// Int8Slice returns a slice of random int8s between min and max, inclusive.
// If min > max, Int8Slice will panic.
// If count is less or euqal to 0, Int8SliceUnique will return an empty slice.
func Int8Slice(min, max, count int8) []int8 {
if count <= 0 {
return []int8{}
}
s := make([]int8, count)
for i := range s {
s[i] = Int8(min, max)
}
return s
}
// Int8SliceUnique returns a slice of unique random int8s between min and max, inclusive.
// If min > max, Int8SliceUnique will panic.
// If count is equal or less than 0, Int8SliceUnique will return an empty slice.
// If count is greater than the number of int8s between min and max, Int8SliceUnique will panic.
func Int8SliceUnique(min, max int8, count int) []int8 {
if count <= 0 {
return []int8{}
}
if count > int(max-min)+1 {
panic("Random int8 slice unique count cannot be greater than the number of int8s between min and max")
}
s, used := make([]int8, count), make(map[int8]bool, count)
for i := 0; i < count; i++ {
randInt := Int8(min, max)
for used[randInt] {
randInt = Int8(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Int16 returns a random int16 between min and max, inclusive.
// If min > max, Int16 panics.
func Int16(min, max int16) int16 {
if min > max {
panic("Random int16 minimum cannot be greater than maximum")
}
return int16(Int32(int32(min), int32(max)))
}
// Int16Slice returns a slice of random int16s between min and max, inclusive.
// If min > max, Int16Slice will panic.
// If count is less or euqal to 0, Int16SliceUnique will return an empty slice.
func Int16Slice(min, max, count int16) []int16 {
if count <= 0 {
return []int16{}
}
s := make([]int16, count)
for i := range s {
s[i] = Int16(min, max)
}
return s
}
// Int16SliceUnique returns a slice of unique random int16s between min and max, inclusive.
// If min > max, Int16SliceUnique will panic.
// If count is equal or less than 0, Int16SliceUnique will return an empty slice.
// If count is greater than the number of int16s between min and max, Int16SliceUnique will panic.
func Int16SliceUnique(min, max int16, count int) []int16 {
if count <= 0 {
return []int16{}
}
if count > int(max-min)+1 {
panic("Random int16 slice unique count cannot be greater than the number of int16s between min and max")
}
s, used := make([]int16, count), make(map[int16]bool, count)
for i := 0; i < count; i++ {
randInt := Int16(min, max)
for used[randInt] {
randInt = Int16(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Int32 returns a random int32 between min and max, inclusive.
// If min > max, Int32 panics.
func Int32(min, max int32) int32 {
if min > max {
panic("Random int32 minimum cannot be greater than maximum")
}
// Handle the edge case where min == 0 and max == math.MaxInt32.
if min == 0 && max == math.MaxInt32 {
return r.Int31()
}
return r.Int31n(max-min+1) + min
}
// Int32Slice returns a slice of random int32s between min and max, inclusive.
// If min > max, Int32Slice will panic.
// If count is less or euqal to 0, Int32SliceUnique will return an empty slice.
func Int32Slice(min, max, count int32) []int32 {
if count <= 0 {
return []int32{}
}
s := make([]int32, count)
for i := range s {
s[i] = Int32(min, max)
}
return s
}
// Int32SliceUnique returns a slice of unique random int32s between min and max, inclusive.
// If min > max, Int32SliceUnique will panic.
// If count is equal or less than 0, Int32SliceUnique will return an empty slice.
// If count is greater than the number of int32s between min and max, Int32SliceUnique will panic.
func Int32SliceUnique(min, max int32, count int) []int32 {
if count <= 0 {
return []int32{}
}
if count > int(max-min)+1 {
panic("Random int32 slice unique count cannot be greater than the number of int32s between min and max")
}
s, used := make([]int32, count), make(map[int32]bool, count)
for i := 0; i < count; i++ {
randInt := Int32(min, max)
for used[randInt] {
randInt = Int32(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Int64 returns a random int64 between min and max, inclusive.
// If min > max, Int64 panics.
func Int64(min, max int64) int64 {
if min > max {
panic("Random int64 minimum cannot be greater than maximum")
}
// Handle the edge case where min == 0 and max == math.MaxInt64.
if min == 0 && max == math.MaxInt64 {
return r.Int63()
}
return r.Int63n(max-min+1) + min
}
// Int64Slice returns a slice of random int64s between min and max, inclusive.
// If min > max, Int64Slice will panic.
// If count is less or euqal to 0, Int64SliceUnique will return an empty slice.
func Int64Slice(min, max, count int64) []int64 {
if count <= 0 {
return []int64{}
}
s := make([]int64, count)
for i := range s {
s[i] = Int64(min, max)
}
return s
}
// Int64SliceUnique returns a slice of unique random int64s between min and max, inclusive.
// If min > max, Int64SliceUnique will panic.
// If count is equal or less than 0, Int64SliceUnique will return an empty slice.
// If count is greater than the number of int64s between min and max, Int64SliceUnique will panic.
func Int64SliceUnique(min, max int64, count int) []int64 {
if count <= 0 {
return []int64{}
}
if count > int(max-min)+1 {
panic("Random int64 slice unique count cannot be greater than the number of int64s between min and max")
}
s, used := make([]int64, count), make(map[int64]bool, count)
for i := 0; i < count; i++ {
randInt := Int64(min, max)
for used[randInt] {
randInt = Int64(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Uint returns a random uint between min and max, inclusive.
// If min > max, Uint will panic.
func Uint(min, max uint) uint {
if min > max {
panic("Random uint minimum cannot be greater than maximum")
}
return uint(Uint64(uint64(min), uint64(max)))
}
// UintSlice returns a slice of random uints between min and max, inclusive.
// If min > max, UintSlice will panic.
// If count is less or euqal to 0, UintSliceUnique will return an empty slice.
func UintSlice(min, max, count uint) []uint {
if count <= 0 {
return []uint{}
}
s := make([]uint, count)
for i := range s {
s[i] = Uint(min, max)
}
return s
}
// UintSliceUnique returns a slice of unique random uints between min and max, inclusive.
// If min > max, UintSliceUnique will panic.
// If count is equal or less than 0, UintSliceUnique will return an empty slice.
// If count is greater than the number of uints between min and max, UintSliceUnique will panic.
func UintSliceUnique(min, max uint, count int) []uint {
if count <= 0 {
return []uint{}
}
if count > int(max-min)+1 {
panic("Random uint slice unique count cannot be greater than the number of uints between min and max")
}
s, used := make([]uint, count), make(map[uint]bool, count)
for i := 0; i < count; i++ {
randInt := Uint(min, max)
for used[randInt] {
randInt = Uint(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Uint8 returns a random uint8 between min and max, inclusive.
// If min > max, Uint8 panics.
func Uint8(min, max uint8) uint8 {
if min > max {
panic("Random uint8 minimum cannot be greater than maximum")
}
return uint8(Uint32(uint32(min), uint32(max)))
}
// Uint8Slice returns a slice of random uint8s between min and max, inclusive.
// If min > max, Uint8Slice will panic.
// If count is less or euqal to 0, Uint8SliceUnique will return an empty slice.
func Uint8Slice(min, max, count uint8) []uint8 {
if count <= 0 {
return []uint8{}
}
s := make([]uint8, count)
for i := range s {
s[i] = Uint8(min, max)
}
return s
}
// Uint8SliceUnique returns a slice of unique random uint8s between min and max, inclusive.
// If min > max, Uint8SliceUnique will panic.
// If count is equal or less than 0, Uint8SliceUnique will return an empty slice.
// If count is greater than the number of uint8s between min and max, Uint8SliceUnique will panic.
func Uint8SliceUnique(min, max uint8, count int) []uint8 {
if count <= 0 {
return []uint8{}
}
if count > int(max-min)+1 {
panic("Random uint8 slice unique count cannot be greater than the number of uint8s between min and max")
}
s, used := make([]uint8, count), make(map[uint8]bool, count)
for i := 0; i < count; i++ {
randInt := Uint8(min, max)
for used[randInt] {
randInt = Uint8(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Uint16 returns a random uint16 between min and max, inclusive.
// If min > max, Uint16 panics.
func Uint16(min, max uint16) uint16 {
if min > max {
panic("Random uint16 minimum cannot be greater than maximum")
}
return uint16(Uint32(uint32(min), uint32(max)))
}
// Uint16Slice returns a slice of random uint16s between min and max, inclusive.
// If min > max, Uint16Slice will panic.
// If count is less or euqal to 0, Uint16SliceUnique will return an empty slice.
func Uint16Slice(min, max, count uint16) []uint16 {
if count <= 0 {
return []uint16{}
}
s := make([]uint16, count)
for i := range s {
s[i] = Uint16(min, max)
}
return s
}
// Uint16SliceUnique returns a slice of unique random uint16s between min and max, inclusive.
// If min > max, Uint16SliceUnique will panic.
// If count is equal or less than 0, Uint16SliceUnique will return an empty slice.
// If count is greater than the number of uint16s between min and max, Uint16SliceUnique will panic.
func Uint16SliceUnique(min, max uint16, count int) []uint16 {
if count <= 0 {
return []uint16{}
}
if count > int(max-min)+1 {
panic("Random uint16 slice unique count cannot be greater than the number of uint16s between min and max")
}
s, used := make([]uint16, count), make(map[uint16]bool, count)
for i := 0; i < count; i++ {
randInt := Uint16(min, max)
for used[randInt] {
randInt = Uint16(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Uint32 returns a random uint32 between min and max, inclusive.
// If min > max, Uint32 panics.
func Uint32(min, max uint32) uint32 {
if min > max {
panic("Random uint32 minimum cannot be greater than maximum")
}
// Handle the edge case where min == 0 and max == math.MaxUint32.
if min == 0 && max == math.MaxUint32 {
return r.Uint32()
}
return r.Uint32()%(max-min+1) + min
}
// Uint32Slice returns a slice of random uint32s between min and max, inclusive.
// If min > max, Uint32Slice will panic.
// If count is less or euqal to 0, Uint32SliceUnique will return an empty slice.
func Uint32Slice(min, max, count uint32) []uint32 {
if count <= 0 {
return []uint32{}
}
s := make([]uint32, count)
for i := range s {
s[i] = Uint32(min, max)
}
return s
}
// Uint32SliceUnique returns a slice of unique random uint32s between min and max, inclusive.
// If min > max, Uint32SliceUnique will panic.
// If count is equal or less than 0, Uint32SliceUnique will return an empty slice.
// If count is greater than the number of uint32s between min and max, Uint32SliceUnique will panic.
func Uint32SliceUnique(min, max uint32, count int) []uint32 {
if count <= 0 {
return []uint32{}
}
if count > int(max-min)+1 {
panic("Random uint32 slice unique count cannot be greater than the number of uint32s between min and max")
}
s, used := make([]uint32, count), make(map[uint32]bool, count)
for i := 0; i < count; i++ {
randInt := Uint32(min, max)
for used[randInt] {
randInt = Uint32(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Uint64 returns a random uint64 between min and max, inclusive.
// If min > max, Uint64 panics.
func Uint64(min, max uint64) uint64 {
if min > max {
panic("Random uint64 minimum cannot be greater than maximum")
}
// Handle the edge case where min is 0 and max is math.MaxUint64.
if min == 0 && max == math.MaxUint64 {
return r.Uint64()
}
return r.Uint64()%(max-min+1) + min
}
// Uint64Slice returns a slice of random uint64s between min and max, inclusive.
// If min > max, Uint64Slice will panic.
// If count is less or euqal to 0, Uint64SliceUnique will return an empty slice.
func Uint64Slice(min, max, count uint64) []uint64 {
if count <= 0 {
return []uint64{}
}
s := make([]uint64, count)
for i := range s {
s[i] = Uint64(min, max)
}
return s
}
// Uint64SliceUnique returns a slice of unique random uint64s between min and max, inclusive.
// If min > max, Uint64SliceUnique will panic.
// If count is equal or less than 0, Uint64SliceUnique will return an empty slice.
// If count is greater than the number of uint64s between min and max, Uint64SliceUnique will panic.
func Uint64SliceUnique(min, max uint64, count int) []uint64 {
if count <= 0 {
return []uint64{}
}
if count > int(max-min)+1 {
panic("Random uint64 slice unique count cannot be greater than the number of uint64s between min and max")
}
s, used := make([]uint64, count), make(map[uint64]bool, count)
for i := 0; i < count; i++ {
randInt := Uint64(min, max)
for used[randInt] {
randInt = Uint64(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Float32 returns a random float32 between min and max, inclusive.
// If min > max, Float32 panics.
func Float32(min, max float32) float32 {
if min > max {
panic("Random float32 minimum cannot be greater than maximum")
}
// Handle the edge case where min is 0 and max is math.MaxFloat32.
if min == 0 && max == math.MaxFloat32 {
return r.Float32()
}
return min + r.Float32()*(max-min)
}
// Float32Slice returns a slice of random float32s between min and max, inclusive.
// If min > max, Float32Slice will panic.
// If count is less or euqal to 0, Float32SliceUnique will return an empty slice.
func Float32Slice(min, max float32, count int) []float32 {
if count <= 0 {
return []float32{}
}
s := make([]float32, count)
for i := range s {
s[i] = Float32(min, max)
}
return s
}
// Float32SliceUnique returns a slice of unique random float32s between min and max, inclusive.
// If min > max, Float32SliceUnique will panic.
// If count is equal or less than 0, Float32SliceUnique will return an empty slice.
// If count is greater than the number of float32s between min and max, Float32SliceUnique will panic.
func Float32SliceUnique(min, max float32, count int) []float32 {
if count <= 0 {
return []float32{}
}
if count > int(max-min)+1 {
panic("Random float32 slice unique count cannot be greater than the number of float32s between min and max")
}
s, used := make([]float32, count), make(map[float32]bool, count)
for i := 0; i < count; i++ {
randInt := Float32(min, max)
for used[randInt] {
randInt = Float32(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}
// Float64 returns a random float64 between min and max, inclusive.
// If min > max, Float64 panics.
func Float64(min, max float64) float64 {
if min > max {
panic("Random float64 minimum cannot be greater than maximum")
}
// Handle the edge case where min is 0 and max is math.MaxFloat64.
if min == 0 && max == math.MaxFloat64 {
return r.Float64()
}
return min + r.Float64()*(max-min)
}
// Float64Slice returns a slice of random float64s between min and max, inclusive.
// If min > max, Float64Slice will panic.
// If count is less or euqal to 0, Float64SliceUnique will return an empty slice.
func Float64Slice(min, max float64, count int) []float64 {
if count <= 0 {
return []float64{}
}
s := make([]float64, count)
for i := range s {
s[i] = Float64(min, max)
}
return s
}
// Float64SliceUnique returns a slice of unique random float64s between min and max, inclusive.
// If min > max, Float64SliceUnique will panic.
// If count is equal or less than 0, Float64SliceUnique will return an empty slice.
// If count is greater than the number of float64s between min and max, Float64SliceUnique will panic.
func Float64SliceUnique(min, max float64, count int) []float64 {
if count <= 0 {
return []float64{}
}
if count > int(max-min)+1 {
panic("Random float64 slice unique count cannot be greater than the number of float64s between min and max")
}
s, used := make([]float64, count), make(map[float64]bool, count)
for i := 0; i < count; i++ {
randInt := Float64(min, max)
for used[randInt] {
randInt = Float64(min, max)
}
used[randInt] = true
s[i] = randInt
}
return s
}