#numbers #generator #generate #bits #bitcoin #truly

bitcoinleveldb-rand

a very simple random number generator. Not especially good at generating truly random bits, but good for basic usage in leveldb

4 releases

0.1.16-alpha.0 Mar 31, 2023
0.1.13-alpha.0 Mar 17, 2023
0.1.12-alpha.0 Jan 19, 2023
0.1.10-alpha.0 Jan 18, 2023

#77 in #generates

Download history 139/week @ 2023-11-28 157/week @ 2023-12-05 164/week @ 2023-12-12 324/week @ 2023-12-19 103/week @ 2023-12-26 133/week @ 2024-01-02 179/week @ 2024-01-09 193/week @ 2024-01-16 112/week @ 2024-01-23 38/week @ 2024-01-30 120/week @ 2024-02-06 155/week @ 2024-02-13 190/week @ 2024-02-20 156/week @ 2024-02-27 133/week @ 2024-03-05 174/week @ 2024-03-12

653 downloads per month
Used in 116 crates (7 directly)

MIT license

265KB
640 lines

bitcoinleveldb-rand

This Rust crate is a direct translation from C++ to Rust of the bitcoin core's random number generator implementation. The crate provides functions for generating random numbers and implementing various random number distribution schemes. Please note that some of the function bodies may still be in the process of translation.

Notice

This crate is part of a direct translation from C++ to Rust of the bitcoin core. As such, some of the function bodies may still be in the process of translation. Please note that this system will become testable once the translation process is complete.

Tokens

Random

The Random struct is used to generate random numbers. It contains a single method next() which returns a random number.

next

The next() method generates a random number.

one_in

The one_in(n: u32) function returns true with a probability of 1/n.

skewed

The skewed(max_log: u32) function generates a skewed random number between 0 and 2^max_log.

uniform

The uniform(max: u32) function generates a uniform random number between 0 and max.

Usage

To use this crate, add bitcoinleveldb-rand as a dependency in your Cargo.toml file:

[dependencies]
bitcoinleveldb-rand = "0.1.0"

Then import the crate in your Rust code:

use bitcoinleveldb_rand::{Random, one_in, skewed, uniform};

fn main() {
    let mut random = Random::new();
    let random_number = random.next();

    if one_in(1000) {
        // do something with probability 1/1000
    }

    // generates a number between 0 and 1023 with
    // a skewed distribution
    let skewed_number = skewed(10); 

    // generates a number between 0 and 99 with
    // a uniform distribution
    let uniform_number = uniform(100); 
}

75> how are random numbers used in leveldb?

Random numbers are used in several places in LevelDB. One of the main uses is in the selection of files for compaction. During compaction, LevelDB merges multiple SSTables into a single SSTable, and random numbers are used to select the files to be merged. This randomization helps to ensure that the data is spread evenly across the SSTables and that the size of the SSTables is balanced, which can improve performance.

Random numbers are also used in the generation of keys and values during benchmarking and testing. This allows for a wide variety of test cases to be generated, which can help to ensure that the system is robust and reliable. Additionally, random numbers are used in other parts of the system where randomness is required, such as in the selection of block sizes and compression ratios.

In general, the use of random numbers in LevelDB helps to ensure that the system is able to handle a wide range of data and workloads, and that performance is optimized for different scenarios.

76> thanks -- what can you tell me about the interface to this crate?

The bitcoinleveldb-rand crate provides a simple interface for generating random numbers. The main type provided by the crate is Random, which represents a source of randomness. The following methods are available on Random:

  • new(seed: u64) creates a new Random object with the given seed.

  • skewed(max_log: u32) -> u32 returns a random integer between 0 and 2^max_log - 1, with skewed distribution towards smaller numbers.

  • uniform(max: u32) -> u32 returns a random integer between 0 and max - 1, with uniform distribution.

  • one_in(n: u32) -> bool returns true with a probability of 1/n.

  • next() -> u32 returns the next random number in the sequence.

These methods can be used to generate random numbers for various purposes in the Leveldb system, such as selecting a random file for compaction or generating keys and values during testing.

Dependencies

~81MB
~801K SLoC