UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RandomStream.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Math/Box.h"
8#include "Math/Vector.h"
9#include "Math/Matrix.h"
10#include "Math/RotationMatrix.h"
11#include "Math/Transform.h"
12#include "HAL/PlatformTime.h"
13
20{
22
23public:
24
31 : InitialSeed(0)
32 , Seed(0)
33 { }
34
44
52 {
53 Initialize(InName);
54 }
55
56public:
57
64 {
65 InitialSeed = InSeed;
67 }
68
75 void Initialize( FName InName )
76 {
77 if (InName != NAME_None)
78 {
79 InitialSeed = GetTypeHash(InName.ToString());
80 }
81 else
82 {
83 InitialSeed = FPlatformTime::Cycles();
84 }
85
86 Seed = uint32(InitialSeed);
87 }
88
92 void Reset() const
93 {
94 Seed = uint32(InitialSeed);
95 }
96
98 {
99 return InitialSeed;
100 }
101
106 {
107 Initialize(FMath::Rand());
108 }
109
115 float GetFraction() const
116 {
117 MutateSeed();
118
119 float Result;
120
121 *(uint32*)&Result = 0x3F800000U | (Seed >> 9);
122
123 return Result - 1.0f;
124 }
125
132 {
133 MutateSeed();
134
135 return Seed;
136 }
137
144 {
145 FVector Result;
147
148 do
149 {
150 // Check random vectors in the unit sphere so result is statistically uniform.
151 Result.X = GetFraction() * 2.f - 1.f;
152 Result.Y = GetFraction() * 2.f - 1.f;
153 Result.Z = GetFraction() * 2.f - 1.f;
154 L = Result.SizeSquared();
155 }
156 while(L > 1.f || L < UE_KINDA_SMALL_NUMBER);
157
158 return Result.GetUnsafeNormal();
159 }
160
167 {
168 return int32(Seed);
169 }
170
177 {
178 return GetFraction();
179 }
180
187 {
188 // GetFraction guarantees a result in the [0,1) range.
189 return ((A > 0) ? FMath::TruncToInt(GetFraction() * float(A)) : 0);
190 }
191
197 inline int32 RandRange( int32 Min, int32 Max ) const
198 {
199 const int32 Range = (Max - Min) + 1;
200
201 return Min + RandHelper(Range);
202 }
203
213
220 {
221 return GetUnitVector();
222 }
223
224 inline FVector RandPointInBox(const FBox& Box) const
225 {
226 return FVector( FRandRange(Box.Min.X, Box.Max.X),
227 FRandRange(Box.Min.Y, Box.Max.Y),
228 FRandRange(Box.Min.Z, Box.Max.Z) );
229 }
230
238 inline FVector VRandCone( FVector const& Dir, float ConeHalfAngleRad ) const
239 {
240 if (ConeHalfAngleRad > 0.f)
241 {
242 float const RandU = FRand();
243 float const RandV = FRand();
244
245 // Get spherical coords that have an even distribution over the unit sphere
246 // Method described at http://mathworld.wolfram.com/SpherePointPicking.html
247 float Theta = 2.f * UE_PI * RandU;
248 float Phi = FMath::Acos((2.f * RandV) - 1.f);
249
250 // restrict phi to [0, ConeHalfAngleRad]
251 // this gives an even distribution of points on the surface of the cone
252 // centered at the origin, pointing upward (z), with the desired angle
253 Phi = FMath::Fmod(Phi, ConeHalfAngleRad);
254
255 // get axes we need to rotate around
256 FMatrix const DirMat = FRotationMatrix(Dir.Rotation());
257 // note the axis translation, since we want the variation to be around X
258 FVector const DirZ = DirMat.GetUnitAxis( EAxis::X );
259 FVector const DirY = DirMat.GetUnitAxis( EAxis::Y );
260
261 FVector Result = Dir.RotateAngleAxis(Phi * 180.f / UE_PI, DirY);
262 Result = Result.RotateAngleAxis(Theta * 180.f / UE_PI, DirZ);
263
264 // ensure it's a unit vector (might not have been passed in that way)
265 Result = Result.GetSafeNormal();
266
267 return Result;
268 }
269 else
270 {
271 return Dir.GetSafeNormal();
272 }
273 }
274
284 {
286 {
287 float const RandU = FRand();
288 float const RandV = FRand();
289
290 // Get spherical coords that have an even distribution over the unit sphere
291 // Method described at http://mathworld.wolfram.com/SpherePointPicking.html
292 float Theta = 2.f * UE_PI * RandU;
293 float Phi = FMath::Acos((2.f * RandV) - 1.f);
294
295 // restrict phi to [0, ConeHalfAngleRad]
296 // where ConeHalfAngleRad is now a function of Theta
297 // (specifically, radius of an ellipse as a function of angle)
298 // function is ellipse function (x/a)^2 + (y/b)^2 = 1, converted to polar coords
300 ConeHalfAngleRad = FMath::Sqrt(1.f / ConeHalfAngleRad);
301
302 // clamp to make a cone instead of a sphere
303 Phi = FMath::Fmod(Phi, ConeHalfAngleRad);
304
305 // get axes we need to rotate around
306 FMatrix const DirMat = FRotationMatrix(Dir.Rotation());
307 // note the axis translation, since we want the variation to be around X
308 FVector const DirZ = DirMat.GetUnitAxis( EAxis::X );
309 FVector const DirY = DirMat.GetUnitAxis( EAxis::Y );
310
311 FVector Result = Dir.RotateAngleAxis(Phi * 180.f / UE_PI, DirY);
312 Result = Result.RotateAngleAxis(Theta * 180.f / UE_PI, DirZ);
313
314 // ensure it's a unit vector (might not have been passed in that way)
315 Result = Result.GetSafeNormal();
316
317 return Result;
318 }
319 else
320 {
321 return Dir.GetSafeNormal();
322 }
323 }
324
330 FString ToString() const
331 {
332 return FString::Printf(TEXT("FRandomStream(InitialSeed=%i, Seed=%u)"), InitialSeed, Seed);
333 }
334
335protected:
336
340 void MutateSeed() const
341 {
342 Seed = (Seed * 196314165U) + 907633515U;
343 }
344
345private:
346
347 // Holds the initial seed.
348 int32 InitialSeed;
349
350 // Holds the current seed. This should be an uint32 so that any shift to obtain top bits
351 // is a logical shift, rather than an arithmetic shift (which smears down the negative bit).
352 mutable uint32 Seed;
353};
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define FVector
Definition IOSSystemIncludes.h:8
#define UE_PI
Definition UnrealMathUtility.h:129
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition NameTypes.h:617
CORE_API FString ToString() const
Definition UnrealNames.cpp:3537
@ Y
Definition Axis.h:14
@ X
Definition Axis.h:13
static uint32 Cycles()
Definition AndroidPlatformTime.h:27
static constexpr UE_FORCEINLINE_HINT T Square(const T A)
Definition UnrealMathUtility.h:578
Definition RandomStream.h:20
int32 GetCurrentSeed() const
Definition RandomStream.h:166
FRandomStream()
Definition RandomStream.h:30
void Reset() const
Definition RandomStream.h:92
void GenerateNewSeed()
Definition RandomStream.h:105
uint32 GetUnsignedInt() const
Definition RandomStream.h:131
void MutateSeed() const
Definition RandomStream.h:340
friend struct Z_Construct_UScriptStruct_FRandomStream_Statics
Definition RandomStream.h:21
FRandomStream(int32 InSeed)
Definition RandomStream.h:40
FVector VRandCone(FVector const &Dir, float ConeHalfAngleRad) const
Definition RandomStream.h:238
void Initialize(int32 InSeed)
Definition RandomStream.h:63
FVector GetUnitVector() const
Definition RandomStream.h:143
FString ToString() const
Definition RandomStream.h:330
void Initialize(FName InName)
Definition RandomStream.h:75
int32 RandRange(int32 Min, int32 Max) const
Definition RandomStream.h:197
UE_FORCEINLINE_HINT FVector::FReal FRandRange(FVector::FReal InMin, FVector::FReal InMax) const
Definition RandomStream.h:209
UE_FORCEINLINE_HINT float FRand() const
Definition RandomStream.h:176
int32 GetInitialSeed() const
Definition RandomStream.h:97
FVector VRandCone(FVector const &Dir, float HorizontalConeHalfAngleRad, float VerticalConeHalfAngleRad) const
Definition RandomStream.h:283
UE_FORCEINLINE_HINT int32 RandHelper(int32 A) const
Definition RandomStream.h:186
UE_FORCEINLINE_HINT FVector VRand() const
Definition RandomStream.h:219
FRandomStream(FName InName)
Definition RandomStream.h:51
float GetFraction() const
Definition RandomStream.h:115
FVector RandPointInBox(const FBox &Box) const
Definition RandomStream.h:224
UE_FORCEINLINE_HINT UE::Math::TRotator< T > Rotation() const
Definition Vector.h:834
double FReal
Definition Vector.h:55
TVector< T > GetSafeNormal(T Tolerance=UE_SMALL_NUMBER, const TVector< T > &ResultIfZero=ZeroVector) const
Definition Vector.h:2060
TVector< T > RotateAngleAxis(const T AngleDeg, const TVector< T > &Axis) const
Definition Vector.h:1335