# Random Integer 64 in Range のバグ？？

**URL:** <https://forums.unrealengine.com/t/random-integer-64-in-range/1568231>\
**Category:** 日本語 (Japanese)\
**Tags:** unreal-engine, UE5, question\
**Created:** [December 28, 2023, 4:06pm UTC](https://forums.unrealengine.com/t/random-integer-64-in-range/1568231 "2023-12-28T16:06:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![betabot2128](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/betabot2128/32/779875_2.png) [@betabot2128](https://forums.unrealengine.com/u/betabot2128)\
**Post date:** [December 28, 2023, 4:06pm UTC](https://forums.unrealengine.com/t/random-integer-64-in-range/1568231/1 "2023-12-28T16:06:15Z")

</div>

上から１段目のノードだと表記通りMinとMaxの間の数からランダムで数が出力されます。しかし２段目のノードはMinがMaxよりも一桁少ないんですが、-1147…のように上限値になったときみたいな表記になります。どうやらMinがMaxよりも桁が少ないとこうなるようです。

３段目と４段目は普通の8bit Integerの上限範囲内で実験してみたのですが通常通りに出力されました。

「これはバグですか？」と質問しても、いつも私の勘違いで終わることが多いのですが、これは今度こそバグでしょうか？

![screenshot. (63)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/4/c/84c081f574182b449f2e6f62a17ec15226ec39c5.jpeg)

 ![screenshot. (62)](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/8/1/881c506676353146282b73026a03e8bead712443.jpeg)

---

<div class="post-metadata">

**Author:** ![betabot2128](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/betabot2128/32/779875_2.png) [@betabot2128](https://forums.unrealengine.com/u/betabot2128)\
**Post date:** [December 28, 2023, 4:07pm UTC](https://forums.unrealengine.com/t/random-integer-64-in-range/1568231/2 "2023-12-28T16:07:35Z")

</div>

ちなみにバージョンは5.21です！

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [December 28, 2023, 5:27pm UTC](https://forums.unrealengine.com/t/random-integer-64-in-range/1568231/3 "2023-12-28T17:27:43Z")

</div>

header

```auto
#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "CustomMath.generated.h"

/**
 * 
 */
UCLASS()
class YOUR_API UCustomMath : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()	
	UFUNCTION(BlueprintCallable)
	static int64 RandomRangeInt64(int64 min, int64 max);
};

```

cpp

```auto
#include "CustomMath.h"
#include <random>

int64 UCustomMath::RandomRangeInt64(int64 min, int64 max) 
{	
	std::random_device rd;
	std::mt19937 rng(rd());
	std::uniform_int_distribution<int64> uni(min, max);
	auto random_integer = uni(rng);
	return random_integer;
}

```

---

<div class="post-metadata">

**Author:** ![kiruru002](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kiruru002](https://forums.unrealengine.com/u/kiruru002)\
**Post date:** [January 16, 2024, 2:04pm UTC](https://forums.unrealengine.com/t/random-integer-64-in-range/1568231/4 "2024-01-16T14:04:38Z")

</div>

こんにちは  
RandHelper64でオーバーフローが起きています

UnrealMathUtility.hを見ると以下のような実装が見つかります

```
UE_NODISCARD static FORCEINLINE int64 RandHelper64(int64 A)
{
	// Note that on some platforms RAND_MAX is a large number so we cannot do ((rand()/(RAND_MAX+1)) * A)
	// or else we may include the upper bound results, which should be excluded.
	return A > 0 ? Min<int64>(TruncToInt(FRand() * (float)A), A - 1) : 0;
}

```

ここで、GenericPlatformMath.hでは、TruncToIntという関数は以下のように実装されています

```
static constexpr FORCEINLINE int32 TruncToInt32(float F)
{
	return (int32)F;
}
static constexpr FORCEINLINE int32 TruncToInt32(double F)
{
	return (int32)F;
}
static constexpr FORCEINLINE int64 TruncToInt64(double F)
{
	return (int64)F;
}

static constexpr FORCEINLINE int32 TruncToInt(float F) { return TruncToInt32(F); }
static constexpr FORCEINLINE int64 TruncToInt(double F) { return TruncToInt64(F); }

```

TruncToInt(float)を見ると、TruncToInt32()を呼び出しているため、この戻り値のビット数が足りずにオーバーフロー発生となります

(このコードはインライン実装のため、念のため手元でコピペしてデバッガーツールも使用してみました)

結論、エンジンのバグですね

unreal issuesも検索したところ[UE-193898](https://issues.unrealengine.com/issue/UE-193898)が見つかりました  
UE5.4で修正予定らしいです

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [February 15, 2024, 2:04pm UTC](https://forums.unrealengine.com/t/random-integer-64-in-range/1568231/5 "2024-02-15T14:04:48Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
