xref: /freebsd/lib/libc/stdlib/random.h (revision e17f5b1d)
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  * $FreeBSD$
26  */
27 
28 #pragma once
29 
30 /*
31  * For each of the currently supported random number generators, we have a
32  * break value on the amount of state information (you need at least this
33  * many bytes of state info to support this random number generator), a degree
34  * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
35  * the separation between the two lower order coefficients of the trinomial.
36  */
37 #define	TYPE_0		0		/* linear congruential */
38 #define	BREAK_0		8
39 #define	DEG_0		0
40 #define	SEP_0		0
41 
42 #define	TYPE_1		1		/* x**7 + x**3 + 1 */
43 #define	BREAK_1		32
44 #define	DEG_1		7
45 #define	SEP_1		3
46 
47 #define	TYPE_2		2		/* x**15 + x + 1 */
48 #define	BREAK_2		64
49 #define	DEG_2		15
50 #define	SEP_2		1
51 
52 #define	TYPE_3		3		/* x**31 + x**3 + 1 */
53 #define	BREAK_3		128
54 #define	DEG_3		31
55 #define	SEP_3		3
56 
57 #define	TYPE_4		4		/* x**63 + x + 1 */
58 #define	BREAK_4		256
59 #define	DEG_4		63
60 #define	SEP_4		1
61 
62 /*
63  * Array versions of the above information to make code run faster --
64  * relies on fact that TYPE_i == i.
65  */
66 #define	MAX_TYPES	5		/* max number of types above */
67 
68 /* A full instance of the random(3) generator. */
69 struct __random_state {
70 	uint32_t	*rst_fptr;
71 	uint32_t	*rst_rptr;
72 	uint32_t	*rst_state;
73 	int		rst_type;
74 	int		rst_deg;
75 	int		rst_sep;
76 	uint32_t	*rst_end_ptr;
77 	/* Flexible array member must be last. */
78 	uint32_t	rst_randtbl[];
79 };
80 
81 struct __random_state *allocatestate(unsigned type);
82 int initstate_r(struct __random_state *, unsigned, uint32_t *, size_t);
83 long random_r(struct __random_state *);
84 void srandom_r(struct __random_state *, unsigned);
85 void srandomdev_r(struct __random_state *);
86