blob: df2a1111e5fe7b09fbeae5093b1522e86ebd53d5 [file] [log] [blame]
Everton Marques871dbcf2009-08-11 15:43:05 -03001/*
2 PIM for Quagga
3 Copyright (C) 2008 Everton da Silva Marques
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19
20 $QuaggaId: $Format:%an, %ai, %h$ $
21*/
22
23#include "pim_rand.h"
24#include "pim_time.h"
25
26/* Quick and dirty random number generator from NUMERICAL RECIPES IN C:
27 THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5). */
28/* BEWARE: '_qseed_' is assigned! */
29#define QRANDOM(_qseed_) ((_qseed_) = (((_qseed_) * 1664525L) + 1013904223L))
30
31static long qpim_rand_seed;
32
33void pim_rand_init()
34{
35 qpim_rand_seed = pim_time_monotonic_sec() ^ getpid();
36}
37
38long pim_rand()
39{
40 return QRANDOM(qpim_rand_seed);
41}
42
43int pim_rand_next(int min, int max)
44{
45 long rand;
46
47 assert(min <= max);
48
49 /* FIXME better random generator ? */
50
51 rand = QRANDOM(qpim_rand_seed);
52 if (rand < 0)
53 rand = -rand;
54 rand = rand % (1 + max - min) + min;
55
56 assert(rand >= min);
57 assert(rand <= max);
58
59 return rand;
60}