GRASS 8 Programmer's Manual 8.6.0dev(2026)-2b93f0dee1
Loading...
Searching...
No Matches
xrand.c
Go to the documentation of this file.
1#include <stdlib.h>
2
3#include <grass/config.h>
4#include <grass/gis.h>
5#include <grass/raster.h>
6#include <grass/calc.h>
7
8/****************************************************************
9rand(lo,hi) random values between a and b
10****************************************************************/
11
12/* The function set by calc_set_random_state(), or NULL. */
13static struct G_random_state *(*random_state)(void);
14
15/*!
16 * \brief Set the function which gives rand() the random number state
17 *
18 * The function must return the state of the calling thread, which the
19 * tool places for the row being evaluated. Without a function, that is,
20 * with NULL, rand() draws from the generator shared by the whole program.
21 *
22 * \param get_state function returning the calling thread's state, or NULL
23 */
25{
26 random_state = get_state;
27}
28
29/* Draw from the state, or from the shared generator without one. */
30static unsigned int draw_uint(struct G_random_state *state)
31{
32 if (state)
33 return G_random_uint32(state);
34 return (unsigned int)G_mrand48();
35}
36
37/* Draw from the state, or from the shared generator without one. */
38static double draw_double(struct G_random_state *state)
39{
40 if (state)
41 return G_random_double(state);
42 return G_drand48();
43}
44
45/*!
46 * \brief Random value between two values, the upper one excluded
47 *
48 * Draws one value per column from the state given by the function set
49 * with calc_set_random_state(), or from the shared generator.
50 */
51int f_rand(int argc, const int *argt, void **args)
52{
53 struct G_random_state *state = random_state ? random_state() : NULL;
54 int i;
55
56 if (argc < 2)
57 return E_ARG_LO;
58 if (argc > 2)
59 return E_ARG_HI;
60
61 switch (argt[0]) {
62 case CELL_TYPE: {
63 CELL *res = args[0];
64 CELL *arg1 = args[1];
65 CELL *arg2 = args[2];
66
67 for (i = 0; i < columns; i++) {
68 unsigned int x = draw_uint(state);
69 int lo = arg1[i];
70 int hi = arg2[i];
71
72 if (lo > hi) {
73 int tmp = lo;
74
75 lo = hi;
76 hi = tmp;
77 }
78 res[i] = (lo == hi) ? lo : (int)(lo + x % (unsigned int)(hi - lo));
79 }
80 return 0;
81 }
82 case FCELL_TYPE: {
83 FCELL *res = args[0];
84 FCELL *arg1 = args[1];
85 FCELL *arg2 = args[2];
86
87 for (i = 0; i < columns; i++) {
88 double x = draw_double(state);
89 FCELL lo = arg1[i];
90 FCELL hi = arg2[i];
91
92 if (lo > hi) {
93 FCELL tmp = lo;
94
95 lo = hi;
96 hi = tmp;
97 }
98 res[i] = (FCELL)(lo + x * (hi - lo));
99 }
100 return 0;
101 }
102 case DCELL_TYPE: {
103 DCELL *res = args[0];
104 DCELL *arg1 = args[1];
105 DCELL *arg2 = args[2];
106
107 for (i = 0; i < columns; i++) {
108 double x = draw_double(state);
109 DCELL lo = arg1[i];
110 DCELL hi = arg2[i];
111
112 if (lo > hi) {
113 DCELL tmp = lo;
114
115 lo = hi;
116 hi = tmp;
117 }
118 res[i] = lo + x * (hi - lo);
119 }
120 return 0;
121 }
122 default:
123 return E_INV_TYPE;
124 }
125}
@ E_INV_TYPE
Definition calc.h:15
@ E_ARG_HI
Definition calc.h:12
@ E_ARG_LO
Definition calc.h:11
int columns
Definition calc.c:5
#define NULL
Definition ccmath.h:32
func_t f_rand
double G_random_double(struct G_random_state *)
Generate a floating-point value in the range [0,1) from a generator of the program's own.
Definition lrand48.c:797
long G_mrand48(void)
Generate an integer in the range [-2^31, 2^31)
Definition lrand48.c:343
uint32_t G_random_uint32(struct G_random_state *)
Generate an integer in the range [0, 2^32)
Definition lrand48.c:815
double G_drand48(void)
Generate a floating-point value in the range [0,1)
Definition lrand48.c:366
float FCELL
Definition gis.h:655
double DCELL
Definition gis.h:654
int CELL
Definition gis.h:653
#define FCELL_TYPE
Definition raster.h:12
#define DCELL_TYPE
Definition raster.h:13
#define CELL_TYPE
Definition raster.h:11
uint64_t state
Definition gis.h:639
#define x
void calc_set_random_state(struct G_random_state *(*get_state)(void))
Set the function which gives rand() the random number state.
Definition xrand.c:24