xref: /dpdk/lib/eal/include/rte_random.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_RANDOM_H_
699a2dd95SBruce Richardson #define _RTE_RANDOM_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * Pseudo-random Generators in RTE
1299a2dd95SBruce Richardson  */
1399a2dd95SBruce Richardson 
14*719834a6SMattias Rönnblom #include <stdint.h>
15*719834a6SMattias Rönnblom 
1699a2dd95SBruce Richardson #ifdef __cplusplus
1799a2dd95SBruce Richardson extern "C" {
1899a2dd95SBruce Richardson #endif
1999a2dd95SBruce Richardson 
2099a2dd95SBruce Richardson /**
2199a2dd95SBruce Richardson  * Seed the pseudo-random generator.
2299a2dd95SBruce Richardson  *
2399a2dd95SBruce Richardson  * The generator is automatically seeded by the EAL init with a timer
2499a2dd95SBruce Richardson  * value. It may need to be re-seeded by the user with a real random
2599a2dd95SBruce Richardson  * value.
2699a2dd95SBruce Richardson  *
2799a2dd95SBruce Richardson  * This function is not multi-thread safe in regards to other
2894278045SMattias Rönnblom  * rte_srand() calls, nor is it in relation to concurrent rte_rand(),
2994278045SMattias Rönnblom  * rte_rand_max() or rte_drand() calls.
3099a2dd95SBruce Richardson  *
3199a2dd95SBruce Richardson  * @param seedval
3299a2dd95SBruce Richardson  *   The value of the seed.
3399a2dd95SBruce Richardson  */
3499a2dd95SBruce Richardson void
3599a2dd95SBruce Richardson rte_srand(uint64_t seedval);
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson /**
3899a2dd95SBruce Richardson  * Get a pseudo-random value.
3999a2dd95SBruce Richardson  *
4099a2dd95SBruce Richardson  * The generator is not cryptographically secure.
4199a2dd95SBruce Richardson  *
4294278045SMattias Rönnblom  * rte_rand(), rte_rand_max() and rte_drand() are multi-thread safe,
4394278045SMattias Rönnblom  * with the exception that they may not be called by multiple
4494278045SMattias Rönnblom  * _unregistered_ non-EAL threads in parallel.
4599a2dd95SBruce Richardson  *
4699a2dd95SBruce Richardson  * @return
4799a2dd95SBruce Richardson  *   A pseudo-random value between 0 and (1<<64)-1.
4899a2dd95SBruce Richardson  */
4999a2dd95SBruce Richardson uint64_t
5099a2dd95SBruce Richardson rte_rand(void);
5199a2dd95SBruce Richardson 
5299a2dd95SBruce Richardson /**
5399a2dd95SBruce Richardson  * Generates a pseudo-random number with an upper bound.
5499a2dd95SBruce Richardson  *
5599a2dd95SBruce Richardson  * This function returns an uniformly distributed (unbiased) random
5699a2dd95SBruce Richardson  * number less than a user-specified maximum value.
5799a2dd95SBruce Richardson  *
5894278045SMattias Rönnblom  * rte_rand(), rte_rand_max() and rte_drand() are multi-thread safe,
5994278045SMattias Rönnblom  * with the exception that they may not be called by multiple
6094278045SMattias Rönnblom  * _unregistered_ non-EAL threads in parallel.
6199a2dd95SBruce Richardson  *
6299a2dd95SBruce Richardson  * @param upper_bound
6399a2dd95SBruce Richardson  *   The upper bound of the generated number.
6499a2dd95SBruce Richardson  * @return
6599a2dd95SBruce Richardson  *   A pseudo-random value between 0 and (upper_bound-1).
6699a2dd95SBruce Richardson  */
6799a2dd95SBruce Richardson uint64_t
6899a2dd95SBruce Richardson rte_rand_max(uint64_t upper_bound);
6999a2dd95SBruce Richardson 
700cd10724SStephen Hemminger /**
710cd10724SStephen Hemminger  * Generates a pseudo-random floating point number.
720cd10724SStephen Hemminger  *
730cd10724SStephen Hemminger  * This function returns a non-negative double-precision floating random
740cd10724SStephen Hemminger  * number uniformly distributed over the interval [0.0, 1.0).
750cd10724SStephen Hemminger  *
760cd10724SStephen Hemminger  * The generator is not cryptographically secure.
777ea0ac41SMattias Rönnblom  *
7894278045SMattias Rönnblom  * rte_rand(), rte_rand_max() and rte_drand() are multi-thread safe,
7994278045SMattias Rönnblom  * with the exception that they may not be called by multiple
8094278045SMattias Rönnblom  * _unregistered_ non-EAL threads in parallel.
810cd10724SStephen Hemminger  *
820cd10724SStephen Hemminger  * @return
830cd10724SStephen Hemminger  *   A pseudo-random value between 0 and 1.0.
840cd10724SStephen Hemminger  */
850cd10724SStephen Hemminger double rte_drand(void);
860cd10724SStephen Hemminger 
8799a2dd95SBruce Richardson #ifdef __cplusplus
8899a2dd95SBruce Richardson }
8999a2dd95SBruce Richardson #endif
9099a2dd95SBruce Richardson 
9199a2dd95SBruce Richardson 
9299a2dd95SBruce Richardson #endif /* _RTE_RANDOM_H_ */
93