-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbrnd.c
61 lines (49 loc) · 1.33 KB
/
vbrnd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* File: vbrnd.c */
#include <stdio.h>
#include <time.h>
#include "vbrnd.h"
/* Declaration Section
*-------------------------------------------------------------------
*/
// Multiplication / division / remainder first adding/subtracting second
// x1 = ( x0 * a + c ) MOD (2^24);
/* [ INFO: How Visual Basic Generates Pseudo-Random Numbers for the RND Function ]
* See Microsoft KB article kb231847
*-----------------------------------------------------------------------------
*/
static unsigned long rndval = 0x50000L;
static unsigned long getrnd(unsigned long val);
/* Public (extern) Functions Section
*-------------------------------------------------------------------
*/
float Rnd(float seed)
{
float rndFloat;
// rndval = (rndval * 0x43fd43fd + 0xc39ec3) & 0xffffff
if (seed < 0)
{
rndval = getrnd((unsigned long)seed);
}
else if (seed > 0)
{
rndval = getrnd(rndval);
}
rndFloat = ((float)rndval / 16777216.0);
return rndFloat;
}
void Randomize(unsigned long seed)
{
if (seed == 0)
{
rndval = time(NULL);
return;
}
rndval = seed;
}
/* Private (static) Functions Section
*-------------------------------------------------------------------
*/
unsigned long getrnd(unsigned long val)
{
return (val * 0x43fd43fdL + 0xc39ec3L) & 0xffffffL;
}