I was hoping there's a built in method in Random for this, but there isn't? So I came up with this function to return the nth instance of a random seed:
public class Program {
static int seed = 123; // This could be anything
static int NextN(int n, int range) // Get the nth instance of seed
{
Random rand = new Random(seed);
List<int> pool = new List<int>();
while( pool.Count< n+1 ){ pool.Add(rand.Next(range)); }
return pool[n];
}
public static void Main()
{
Console.WriteLine(NextN(8,100)); // Return 19 as the 8th value of seed 123 at 100 range.
}}
As you can see, the NextN() function generate the sequence of random numbers first, then return the value of the nth instance. This will perform badly once you're trying to reach a very high instance (like in the millions). Since random always return the same value each time you restart an app, I need to keep track how many times the random seed is accessed between session. is there a way around this?
Aucun commentaire:
Enregistrer un commentaire