trident_fuzz/trident/
random.rs

1use rand::distributions::uniform::SampleRange;
2use rand::distributions::uniform::SampleUniform;
3use solana_sdk::pubkey::Pubkey;
4use solana_sdk::signature::Keypair;
5
6use crate::trident::Trident;
7
8impl Trident {
9    /// Generates a random value within the specified range
10    ///
11    /// This method uses the internal RNG to generate a random value of type T
12    /// within the given range. The range can be inclusive or exclusive.
13    ///
14    /// # Arguments
15    /// * `range` - The range to sample from (e.g., 0..10, 0..=9)
16    ///
17    /// # Returns
18    /// A random value of type T within the specified range
19    ///
20    /// # Example
21    /// ```rust, ignore
22    /// let random_u64 = trident.random_from_range(1..=100);
23    /// let random_f64 = trident.random_from_range(0.0..1.0);
24    /// ```
25    pub fn random_from_range<T, R>(&mut self, range: R) -> T
26    where
27        T: SampleUniform,
28        R: SampleRange<T>,
29    {
30        self.rng.gen_range(range)
31    }
32
33    /// Generates a random Solana public key
34    ///
35    /// Creates a cryptographically random 32-byte public key,
36    /// useful for generating test accounts and addresses.
37    ///
38    /// # Returns
39    /// A randomly generated Pubkey
40    pub fn random_pubkey(&mut self) -> Pubkey {
41        self.rng.gen_pubkey()
42    }
43
44    /// Generates a random string of the specified length
45    ///
46    /// Creates a random string containing alphanumeric characters,
47    /// useful for generating test data like names, symbols, or URIs.
48    ///
49    /// # Arguments
50    /// * `length` - The desired length of the generated string
51    ///
52    /// # Returns
53    /// A random string of the specified length
54    pub fn random_string(&mut self, length: usize) -> String {
55        self.rng.gen_string(length)
56    }
57
58    /// Fills a byte slice with random data
59    ///
60    /// This method fills the provided mutable byte slice with
61    /// cryptographically random data, useful for generating
62    /// random seeds, nonces, or other binary data.
63    ///
64    /// # Arguments
65    /// * `bytes` - A mutable byte slice to fill with random data
66    pub fn random_bytes(&mut self, bytes: &mut [u8]) {
67        self.rng.fill_bytes(bytes);
68    }
69
70    /// Generates a random boolean value
71    ///
72    /// Creates a random boolean value, useful for testing with boolean inputs.
73    ///
74    /// # Returns
75    /// A random boolean value
76    ///
77    /// # Example
78    /// ```rust, ignore
79    /// let random_bool = trident.random_bool();
80    /// ```
81    pub fn random_bool(&mut self) -> bool {
82        self.rng.gen_bool()
83    }
84
85    /// Generates a random Solana keypair
86    ///
87    /// Creates a cryptographically secure random Ed25519 keypair,
88    /// useful for generating test signers, authority accounts, and
89    /// testing signature verification.
90    ///
91    /// # Returns
92    /// A randomly generated Keypair with a valid public/private key pair
93    ///
94    /// # Example
95    /// ```rust, ignore
96    /// let signer = trident.random_keypair();
97    /// let authority = trident.random_keypair();
98    /// ```
99    pub fn random_keypair(&mut self) -> Keypair {
100        self.rng.gen_keypair()
101    }
102}