ByteHelper
@buff-beacon-project/curby-client • Docs
@buff-beacon-project/curby-client / ByteHelper
Interface: ByteHelper
A helper for working with randomness.
Contains methods for reading bits, shuffling arrays, and more.
Example
// BytesHelper from a randomness call
const randomness = await client.randomness()
// Get a list of unsigned numbers formed by reading 10 bits at a time
const bits = Array.from(randomness.bits(10))
// Get a shuffled version of an array
const array = [1, 2, 3, 4, 5]
const shuffled = randomness.shuffled(array)
// shuffled = [3, 1, 5, 2, 4]
Properties
Methods
applyShuffle()
applyShuffle(
arr
):void
Applies a shuffle to an array
Parameters
arr
any
[]
The array to shuffle
Returns
void
Example
const array = [1, 2, 3, 4, 5]
randomness.applyShuffle(array)
// array = [3, 1, 5, 2, 4]
Defined in
bits()
bits(
n
):Generator
<number
,any
,any
>
A generator for reading unsigned numbers
Parameters
n
number
The number of bits to read at a time
Returns
Generator
<number
, any
, any
>
Example
for (const n of randomness.bits(10)){
console.log(n)
}
// or create an array
const numbers = Array.from(randomness.bits(10))
Defined in
bytes()
bytes():
Uint8Array
The bytes of the randomness
Returns
Uint8Array
Defined in
reader()
reader():
BitReader
The a BitReader for the randomness
Returns
BitReader
See
https://github.com/buff-beacon-project/rand-utils
Defined in
shuffled()
shuffled(
arr
):any
[]
Shuffles an array using the randomness
Parameters
arr
any
[]
The array to shuffle
Returns
any
[]
Example
const array = [1, 2, 3, 4, 5]
const shuffled = randomness.shuffled(array)
// shuffled = [3, 1, 5, 2, 4]
Defined in
signedBits()
signedBits(
n
):Generator
<number
,any
,any
>
A generator for reading signed numbers
Parameters
Returns
Generator
<number
, any
, any
>
Defined in
stream()
stream():
BitStream
The BitStream for the randomness
Returns
BitStream
Defined in
unfold()
unfold(
cb
):Generator
<any
,any
,any
>
Unfolds the randomness
Parameters
cb
(reader
, index
) => any
A function to call for each iteration
Returns
Generator
<any
, any
, any
>
Example
// A random walk
const cardinal = ['north', 'east', 'south', 'west']
const path = randomness.unfold((reader) => {
const n = reader.readBits(2, false)
return cardinal[n]
})
for (const step of path){
console.log(step)
}
Defined in
Last updated