New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / include / rte_random.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_RANDOM_H_
6 #define _RTE_RANDOM_H_
7
8 /**
9  * @file
10  *
11  * Pseudo-random Generators in RTE
12  */
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #include <stdint.h>
19 #include <stdlib.h>
20
21 /**
22  * Seed the pseudo-random generator.
23  *
24  * The generator is automatically seeded by the EAL init with a timer
25  * value. It may need to be re-seeded by the user with a real random
26  * value.
27  *
28  * @param seedval
29  *   The value of the seed.
30  */
31 static inline void
32 rte_srand(uint64_t seedval)
33 {
34         srand48((long)seedval);
35 }
36
37 /**
38  * Get a pseudo-random value.
39  *
40  * This function generates pseudo-random numbers using the linear
41  * congruential algorithm and 48-bit integer arithmetic, called twice
42  * to generate a 64-bit value.
43  *
44  * @return
45  *   A pseudo-random value between 0 and (1<<64)-1.
46  */
47 static inline uint64_t
48 rte_rand(void)
49 {
50         uint64_t val;
51         val = (uint64_t)lrand48();
52         val <<= 32;
53         val += (uint64_t)lrand48();
54         return val;
55 }
56
57 #ifdef __cplusplus
58 }
59 #endif
60
61
62 #endif /* _RTE_RANDOM_H_ */