Created
December 2, 2023 17:47
-
-
Save DevEarley/0e1829e377ed2fd95a8c719ef26e679e to your computer and use it in GitHub Desktop.
C# RNG implementation for Unity3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RNGController : MonoBehaviour | |
{ | |
private int index = 0; | |
private int lastLargestIndex = 0; | |
private int max_index = 1000; | |
private List <int> values = new List<int>(); | |
public int Next(int RNG_ID) | |
{ | |
var offsetIndex = RNG_ID + index; | |
if (offsetIndex >= max_index) { | |
offsetIndex = offsetIndex - max_index; | |
} | |
lastLargestIndex = Mathf.Max(lastLargestIndex,offsetIndex); | |
return values[offsetIndex]; | |
} | |
public bool PollRNG (RNGPoll poll) | |
{ | |
poll.timer += Time.deltaTime; | |
if(poll.timer > poll.pollingInterval) | |
{ | |
poll.timer = poll.timer - poll.pollingInterval; | |
var number = Next(poll.RNG_ID); | |
if(number < poll.sucessRate * max_index) | |
{ | |
return true; | |
} | |
return false; | |
} | |
return false; | |
} | |
void Awake() | |
{ | |
for(var i = 0;i<max_index;i++) | |
{ | |
values.Add(i); | |
} | |
values = RNGService.Shuffle(values); | |
} | |
void LateUpdate() | |
{ | |
index = lastLargestIndex; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class RNGPoll | |
{ | |
public RNGPoll( float _pollingInterval, float _sucessRate, int _RNG_ID) | |
{ | |
pollingInterval = _pollingInterval; | |
sucessRate = _sucessRate; | |
RNG_ID = _RNG_ID; | |
} | |
public float pollingInterval; | |
public float sucessRate; | |
public float timer = 0.0f; | |
public int RNG_ID; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public static class RNGService | |
{ | |
public static List<int> Shuffle (List<int> list) | |
{ | |
for(var i = 0;i<list.Count;i++) | |
{ | |
decimal a = i * 15485863; | |
a = (a * a * a % 2038074743) / 2038074743; | |
if(a<0)a*=-1; | |
var fa = Mathf.Clamp01((float)a); | |
int aIndex = (int)(fa * list.Count); | |
int value = list[aIndex]; | |
list[aIndex] = list[i]; | |
list[i] = value; | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment