1

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Vectorinx
  • 59
  • 3
  • 3
    Why do you specify the seed? Leave it empty and it uses time as seed which is different each time – Emad Dec 18 '19 at 05:49
  • 1
    This may help: https://stackoverflow.com/questions/19512210/how-to-save-the-state-of-a-random-generator-in-c – Kevin Gosse Dec 18 '19 at 05:51
  • 1
    Are you looking for a way to create truly random numbers? What's your ultimate goal? – Aydin Dec 18 '19 at 05:52
  • 1
    I think the goal is to have a deterministic set of random numbers even after restarting the app. It's often used for procedurally generated content, or in videogames to prevent players from cheating the RNG by saving/reloading until they get the result they want – Kevin Gosse Dec 18 '19 at 05:54
  • Only the seed is randomed at first, then it's locked between session. Then I need a fixed (random) seqeunce from the seed. *Just like Kevin Gosse said, to prevent cheating. – Vectorinx Dec 18 '19 at 05:55
  • Closing as dupe then, this is the exact same need as the question I linked. – Kevin Gosse Dec 18 '19 at 05:56
  • Wasn't the link is about saving the Random state? I'm trying to use this as a standalone. – Vectorinx Dec 18 '19 at 06:01
  • I'm not sure I understand the nuance, but I gotta run so I'm reopening it and leaving this to whoever's around :) – Kevin Gosse Dec 18 '19 at 06:03
  • Will the range change between sessions? If not, why is it passed as a parameter? – Johan Donne Dec 18 '19 at 06:16
  • it's just an example, but I prefer it to be dynamic later. – Vectorinx Dec 18 '19 at 06:49
  • In .Net Framework you could just serialize your Random instance between sessions. Did you try that? Or are you targetting core? – Johan Donne Dec 18 '19 at 07:46
  • Saving random's state is exactly how games would do it. I don't see reason why you would not do it like that. – Euphoric Dec 18 '19 at 08:39
  • I'm looking for a way to do this in a single clean method. If it's not possible without saving the random state, guess it'll have to do. – Vectorinx Dec 18 '19 at 11:58
  • Why are you storing "n-1" values that you ignore anyway? Just overwrite a single int value in the loop and use its latest value. – Hans Kesting Dec 19 '19 at 14:37

0 Answers0