00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#ifndef RANDOM_H
00022
#define RANDOM_H
00023
00024
#include "types.h"
00025
#include "vector.h"
00026
00031 class Random {
00032
00033
public:
00034
Random(
long s = 0) {randx = s;}
00035
void seed(
long s) {randx = s;}
00036
00037
static Random global;
00038
00039
public:
00040
00043 DBL
getDouble() {
return abs(draw())/max();}
00044
00047 DBL
jitterFactor(DBL amouth) {
00048
return 1-amouth+2*amouth*
getDouble();
00049 }
00050
00051
00054 Vector3 pointInBox() {
00055 DBL d1 =
getDouble();
00056 DBL d2 =
getDouble();
00057 DBL d3 =
getDouble();
00058
return Vector3(2*d1-1,2*d2-1,2*d3-1);
00059 }
00060
00063 Vector3 pointInSphere() {
00064
Vector3 ret;
00065
do {
00066 ret =
pointInBox();
00067 }
while (ret.
lengthSq()>1);
00068
return ret;
00069 }
00070
00073 Vector3 direction() {
00074
Vector3 ret;
00075
do{ret =
pointInSphere();}
while(ret ==
Vector3(0));
00076 ret.
normalize();
00077
return ret;
00078 }
00079
00080
private:
00081
unsigned long randx;
00082
static long abs(
long x) {
return x&0x7fffffff;}
00083
static double max() {
return 2147483648.0;}
00084
long draw() {
return randx = randx*1103515245 + 12345;}
00085
00086 };
00087
00088
#endif