Random Number Generator (RNG) is a crucial component of online casino games and many other digital applications that require randomness. An RNG is a software algorithm designed to generate a sequence of numbers or outcomes that appear to be random and unpredictable.
Add the Fireball RNG into you game script in Game Server function
public class Game : IGame
{
private readonly IFireballLogger _logger;
private readonly IFireball _fireball;
private readonly IRng _fireballRng;
public Game(ILogger<Game> logger, IFireball fireball, IRng fireballRng)
{
_logger = new FireballLogger(nameof(Game), logger);
_fireball = fireball;
_fireballRng = fireballRng;
}
...
}
! NOTE: Do not forget to add next RNG enviroment variables when creating new the Server version inside Fireball Admin panel in Game Page -> Server tab
| Key | Value |
|---|---|
RNG_BUFFER_SIZE |
100 |
RNG_URL |
https://cloud.fireballserver.com/rng |
For get any random value, you can use any of this methods
var random = await _fireballRng.NextInt(minInt, maxInt);
var random = await _fireballRng.NextDouble(minDouble, maxDouble);
var random = await _fireballRng.NextBool();
Additionally, you can randomly shuffle any list by using:
// create list of any type
var list = new List<string>()
{
"one", "two", "three"
};
// randomlly shuffle list
await _fireballRng.Shuffle(list);
or get random element index from weighted array:
public class ElementData
{
public string Id;
public float Chance;
public ElementData(string id, float chance)
{
Id = id;
Chance = chance;
}
}
....
// game data object example
var elements = new List<ElementData>()
{
new ElementData("ace", 0.1f ),
new ElementData("king", 0.2f ),
new ElementData("queen", 0.3f ),
new ElementData("jack", 0.4f ),
};
// select a weight list from game data
var weigths = elements.Select(w => (double)w.Chance).ToList();
// randomly select the weighted element from the list
int index = await _fireballRng.IndexByWeights(weigths);
// result
var random = elements[index];