Scoop API

scoop:scoop.std:0.1.0

src/random/random.scoop

Selected production source · src/random/random.scoop

1package scoop.random
3/**
4 * A random-bit source with bounded sampling and byte helpers. Cryptographic suitability is
5 * determined by the implementation; seeded generators are not cryptographic.
6 */
7public interface Random {
8 /**
9 * Produces 0..32 random bits in an Int result; implementations reject counts outside that
10 * range.
11 */
12 public fun nextBits(bitCount: Int): Int
14 /**
15 * Samples one random bit as a Boolean.
16 */
17 public fun nextBoolean(): Boolean = nextBits(1) != 0
18 /**
19 * Samples an Int; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
20 * with zero as the default lower bound.
21 */
22 public fun nextInt(): Int = nextBits(32)
23 /**
24 * Samples a UInt; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
25 * with zero as the default lower bound.
26 */
27 public fun nextUInt(): UInt = nextBits(32).toUInt()
28 /**
29 * Samples a ULong; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
30 * with zero as the default lower bound.
31 */
32 public fun nextULong(): ULong {
33 val high = nextBits(32).toUInt().toULong()
34 val low = nextBits(32).toUInt().toULong()
35 return (high shl 32L) or low
36 }
37 /**
38 * Samples a Long; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
39 * with zero as the default lower bound.
40 */
41 public fun nextLong(): Long = nextULong().toLong()
42 /**
43 * Samples a Float in [0, 1) using 24 random bits.
44 */
45 public fun nextFloat(): Float = nextBits(24).toFloat() / 16777216f
46 /**
47 * Samples a Double in [0, 1) using 53 random bits.
48 */
49 public fun nextDouble(): Double {
50 val high = nextBits(26).toULong()
51 val low = nextBits(27).toULong()
52 return ((high shl 27L) or low).toDouble() / 9007199254740992.0
53 }
55 /**
56 * Samples an Int; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
57 * with zero as the default lower bound.
58 */
59 public fun nextInt(until: Int): Int = nextInt(0, until)
60 /**
61 * Samples an Int; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
62 * with zero as the default lower bound.
63 */
64 public fun nextInt(from: Int, until: Int): Int {
65 if (from >= until) throw IllegalArgumentException(Some("empty random range"))
66 val offset = sampleUInt(this, until.toUInt() - from.toUInt())
67 return (from.toUInt() + offset).toInt()
68 }
69 /**
70 * Samples a UInt; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
71 * with zero as the default lower bound.
72 */
73 public fun nextUInt(until: UInt): UInt = nextUInt(0u, until)
74 /**
75 * Samples a UInt; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
76 * with zero as the default lower bound.
77 */
78 public fun nextUInt(from: UInt, until: UInt): UInt {
79 if (from >= until) throw IllegalArgumentException(Some("empty random range"))
80 return from + sampleUInt(this, until - from)
81 }
82 /**
83 * Samples a Long; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
84 * with zero as the default lower bound.
85 */
86 public fun nextLong(until: Long): Long = nextLong(0L, until)
87 /**
88 * Samples a Long; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
89 * with zero as the default lower bound.
90 */
91 public fun nextLong(from: Long, until: Long): Long {
92 if (from >= until) throw IllegalArgumentException(Some("empty random range"))
93 val offset = sampleULong(this, until.toULong() - from.toULong())
94 return (from.toULong() + offset).toLong()
95 }
96 /**
97 * Samples a ULong; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
98 * with zero as the default lower bound.
99 */
100 public fun nextULong(until: ULong): ULong = nextULong(0uL, until)
101 /**
102 * Samples a ULong; bounded overloads use a lower-inclusive, upper-exclusive nonempty range,
103 * with zero as the default lower bound.
104 */
105 public fun nextULong(from: ULong, until: ULong): ULong {
106 if (from >= until) throw IllegalArgumentException(Some("empty random range"))
107 return from + sampleULong(this, until - from)
108 }
110 /**
111 * Produces a new byte array, or fills the supplied array in the half-open index range and
112 * returns it. Negative sizes and invalid ranges throw.
113 */
114 public fun nextBytes(size: Long): Array<Byte> {
115 if (size < 0L) throw IllegalArgumentException(Some("negative byte count"))
116 return Array<Byte>(size) { _ -> nextBits(8).toByte() }
117 }
118 /**
119 * Produces a new byte array, or fills the supplied array in the half-open index range and
120 * returns it. Negative sizes and invalid ranges throw.
121 */
122 public fun nextBytes(array: MutableArray<Byte>, fromIndex: Long = 0L, toIndex: Long = array.size): MutableArray<Byte> {
123 if (fromIndex < 0L || toIndex < 0L || fromIndex > array.size || toIndex > array.size) throw IndexOutOfBoundsException()
124 if (fromIndex > toIndex) throw IllegalArgumentException(Some("reversed byte range"))
125 var index = fromIndex
126 while (index < toIndex) { array[index] = nextBits(8).toByte(); index += 1L }
127 return array
128 }
130 /**
131 * Factories for deterministic pseudorandom generators.
132 */
133 public companion object {
134 /**
135 * Creates a repeatable xoshiro256** stream from the supplied seed; it is not
136 * cryptographically secure.
137 */
138 public fun seeded(seed: Long): Random = Xoshiro256StarStar(seed)
139 }