Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
Random3Class.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE__RANDOM3CLASS_H
13#define TT_INCLUDE__RANDOM3CLASS_H
14template<class T>
15int Pick_Random_Number(T & generator, int minval, int maxval)
16{
17 if (minval == maxval) return(minval);
18 if (minval > maxval)
19 {
20 int temp = minval;
21 minval = maxval;
22 maxval = temp;
23 }
24 int magnitude = maxval - minval;
25 int highbit = T::SIGNIFICANT_BITS-1;
26 while ((magnitude & (1 << highbit)) == 0 && highbit > 0)
27 {
28 highbit--;
29 }
30 int mask = ~( (~0L) << (highbit+1));
31 int pick = magnitude+1;
32 while (pick > magnitude)
33 {
34 pick = generator() & mask;
35 }
36 return(pick + minval);
37}
38
39class Random3Class
40{
41public:
42 Random3Class(unsigned seed1 = 0, unsigned seed2 = 0);
43 operator int(void)
44 {
45 return(operator()());
46 }
47 int operator() (void);
48 int operator() (int minval, int maxval);
49 enum {
50 SIGNIFICANT_BITS=32
51 };
52protected:
53 static int Mix1[20];
54 static int Mix2[20];
55 int Seed;
56 int Index;
57};
58#endif