xref: /freebsd/lib/libc/stdlib/random.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * Copyright 2020 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #pragma once
27 
28 /*
29  * For each of the currently supported random number generators, we have a
30  * break value on the amount of state information (you need at least this
31  * many bytes of state info to support this random number generator), a degree
32  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
33  * the separation between the two lower order coefficients of the trinomial.
34  */
35 #define	TYPE_0		0		/* linear congruential */
36 #define	BREAK_0		8
37 #define	DEG_0		0
38 #define	SEP_0		0
39 
40 #define	TYPE_1		1		/* x**7 + x**3 + 1 */
41 #define	BREAK_1		32
42 #define	DEG_1		7
43 #define	SEP_1		3
44 
45 #define	TYPE_2		2		/* x**15 + x + 1 */
46 #define	BREAK_2		64
47 #define	DEG_2		15
48 #define	SEP_2		1
49 
50 #define	TYPE_3		3		/* x**31 + x**3 + 1 */
51 #define	BREAK_3		128
52 #define	DEG_3		31
53 #define	SEP_3		3
54 
55 #define	TYPE_4		4		/* x**63 + x + 1 */
56 #define	BREAK_4		256
57 #define	DEG_4		63
58 #define	SEP_4		1
59 
60 /*
61  * Array versions of the above information to make code run faster --
62  * relies on fact that TYPE_i == i.
63  */
64 #define	MAX_TYPES	5		/* max number of types above */
65 
66 /* A full instance of the random(3) generator. */
67 struct __random_state {
68 	uint32_t	*rst_fptr;
69 	uint32_t	*rst_rptr;
70 	uint32_t	*rst_state;
71 	int		rst_type;
72 	int		rst_deg;
73 	int		rst_sep;
74 	uint32_t	*rst_end_ptr;
75 	/* Flexible array member must be last. */
76 	uint32_t	rst_randtbl[];
77 };
78 
79 struct __random_state *allocatestate(unsigned type);
80 int initstate_r(struct __random_state *, unsigned, uint32_t *, size_t);
81 long random_r(struct __random_state *);
82 void srandom_r(struct __random_state *, unsigned);
83 void srandomdev_r(struct __random_state *);
84