CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
OdRandom.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, Open Design Alliance (the "Alliance").
3// All rights reserved.
4//
5// This software and its documentation and related materials are owned by
6// the Alliance. The software may only be incorporated into application
7// programs owned by members of the Alliance, subject to a signed
8// Membership Agreement and Supplemental Software License Agreement with the
9// Alliance. The structure and organization of this software are the valuable
10// trade secrets of the Alliance and its suppliers. The software is also
11// protected by copyright law and international treaty provisions. Application
12// programs incorporating this software must include the following statement
13// with their copyright notices:
14//
15// This application incorporates Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 by Open Design Alliance.
18// All rights reserved.
19//
20// By use of this software, its documentation or related materials, you
21// acknowledge and accept the above terms.
23#ifndef _OD_RANDOM_H_
24#define _OD_RANDOM_H_
25
26#include "OdaCommon.h"
27#include "OdArray.h"
28
35public:
36 virtual ~OdRandomGen();
37 virtual OdRandomGen *clone() = 0;
38
39 // The future sequence is fully determined by the provided seed.
40 // Note: do NOT pass nondeterministic things like time(), otherwise your results won't be reproducible!
41 // In most cases you should NOT call it: every generator has a fixed default value.
42 virtual void setSeed(OdUInt32 seed) = 0;
43 virtual OdUInt32 getSeed() const = 0;
44
45 virtual void getRange(OdUInt32& high) const = 0;
46 virtual OdUInt32 generate() = 0;
47};
48
49
66public:
67 // If you pass custom random generator, then OdRandom receives ownership over it.
68 // Otherwise, a default OdRandomGenMinstd generator will be created.
69 explicit OdRandom(OdRandomGen *newGenerator = NULL);
70
72 OdRandom(const OdRandom &iSrc);
73 void operator=(const OdRandom &iSrc);
74
76 const OdRandomGen &generator() const;
77
78 //note: all integer-valued methods are inclusive, i.e. low <= return <= high
82 int genInt(int low, int high);
83
84 bool genBool();
85
86 //note: real-valued methods are potentially inclusive too, i.e. low <= return <= high
87 double genDouble();
88 double genDouble(double low, double high);
89
90 template <class T, class A> const T& randomOf(const OdArray<T, A>& iArray) {
91 int idx = genInt(1, iArray.size()) - 1;
92 return iArray[idx];
93 }
94
95private:
96 OdUInt64 genAnyInt();
97
98 //owning the generator
99 OdRandomGen *m_generator;
100 //cache: avoid calling virtual functions too much
101 OdUInt32 m_genMax, m_genCalls;
102};
103
104//shuffles the given array, so that all possible outcomes are equiprobable
105template<class T> void OdRandomShuffle(T *beg, T *end, OdRandom *rnd) {
106 ODA_ASSERT(end >= beg);
108 size_t n = end - beg;
109 for (size_t i = 0; i < n; i++) {
110 size_t pos = (size_t)rnd->genUInt64(0, i);
111 { //TODO: is there anything like std::swap ?
112 T buff = beg[pos];
113 beg[pos] = beg[i];
114 beg[i] = buff;
115 }
116 }
117}
118
119//======================== implementations of random number generators ========================
120
126public:
130
131 virtual void setSeed(OdUInt32 seed);
132 virtual OdUInt32 getSeed() const;
133
134 virtual void getRange(OdUInt32& high) const;
136
137private:
138 OdUInt32 m_state;
139};
140
141#endif
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:57
rnd
Definition: DimVarDefs.h:1591
unsigned int OdUInt32
void OdRandomShuffle(T *beg, T *end, OdRandom *rnd)
Definition: OdRandom.h:105
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
size_type size() const
Definition: OdArray.h:1247
Definition: Int64.h:43
virtual void getRange(OdUInt32 &high) const =0
virtual OdUInt32 generate()=0
virtual ~OdRandomGen()
virtual OdUInt32 getSeed() const =0
virtual OdRandomGen * clone()=0
virtual void setSeed(OdUInt32 seed)=0
OdRandomGenMinstd(OdUInt32 seed=1)
virtual void getRange(OdUInt32 &high) const
virtual void setSeed(OdUInt32 seed)
virtual ~OdRandomGenMinstd()
virtual OdRandomGenMinstd * clone()
virtual OdUInt32 generate()
virtual OdUInt32 getSeed() const
double genDouble(double low, double high)
OdUInt32 genUInt(OdUInt32 low, OdUInt32 high)
OdRandomGen & generator()
OdRandom(OdRandomGen *newGenerator=NULL)
OdUInt64 genUInt64(OdUInt64 low, OdUInt64 high)
const T & randomOf(const OdArray< T, A > &iArray)
Definition: OdRandom.h:90
const OdRandomGen & generator() const
double genDouble()
void operator=(const OdRandom &iSrc)
OdInt64 genInt64(OdInt64 low, OdInt64 high)
bool genBool()
int genInt(int low, int high)
OdRandom(const OdRandom &iSrc)