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
6#include <cstring> // for memcpy().
7
8namespace uLang
9{
10
16{
17public:
18
21 : _Seed(Seed)
22 {}
23
29 {
30 _Seed = uint32_t(Seed);
31 }
32
35 {
36 MutateSeed();
37
38 const int64_t Range = int64_t(Max) - int64_t(Min) + 1;
39 return Min + uint32_t(Range == 0 ? 0 : (int64_t(_Seed) % Range));
40 }
41
43 float FRandRange(float Min, float Max)
44 {
45 MutateSeed();
46
47 uint32_t UnitFractionAsInt = 0x3F800000U | (_Seed >> 9);
48 float UnitFraction;
49 memcpy(&UnitFraction, &UnitFractionAsInt, sizeof(float));
50
51 return Min + (UnitFraction - 1.0f) * (Max - Min);
52 }
53
55 bool RandBool()
56 {
57 MutateSeed();
58 return !!(_Seed & 1);
59 }
60
61protected:
62
65 {
66 _Seed = (_Seed * 196314165U) + 907633515U;
67 }
68
69private:
70
71 // Holds the current seed. This should be an uint32 so that any shift to obtain top bits
72 // is a logical shift, rather than an arithmetic shift (which smears down the negative bit).
73 uint32_t _Seed;
74};
75
76}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
memcpy(InputBufferBase, BinkBlocksData, BinkBlocksSize)
Definition RandomStream.h:16
CRandomStream(int32_t Seed=0)
Definition RandomStream.h:20
bool RandBool()
Definition RandomStream.h:55
float FRandRange(float Min, float Max)
Definition RandomStream.h:43
void Initialize(int32_t Seed)
Definition RandomStream.h:28
void MutateSeed()
Definition RandomStream.h:64
int32_t RandRange(int32_t Min, int32_t Max)
Definition RandomStream.h:34
Definition VVMEngineEnvironment.h:23