Reputation: 31
Edit 2: New explanation since it looks like my question are quite vague. My question are already answered by user boriaz50:
- I want a random-like function where if i input two set of seed/key it will return a static set of value which be converted into ranged number (ex 1-99) using Pseudorandom number generator (PNRG) algorithm (Answered by user boriaz50. See his linear congruential generator code for his example or below for it's modification).
- The reason i don't use System.Random is because i can't set the second seed/key of that function. System.Random seems to use "0" as the second seed/key and everytime i use Next() that key increased by one. This mean if i change the position or amount of line that use Next() function then the whole result will change (Making there's no point remembering the seed on the next program build). Edit 3: Just realize in boriaz50 first code call "new Random" everytime, its seed/key keep resetting making it a workaway solution.
public int RandomGenerator(int seed1, int seed2, int minRange, int maxRange) { long temp = seed1; for(int i = 0; i < seed2; i++) { temp = (48271 * temp) % int.MaxValue; } int result = (int)(minRange + temp % maxRange); return result; }
How do to get the same number every time assuming i have two seed and i want to get a random value between 1-99.
In c# there is a System.Random function but it use next() which mean i can't guess what number that came out assuming i know both seed. Is there another function in c# that able to accomplish my goal?
int gameSeed = 123;
public void GenerateAgeList(){
GenerateAge(1); //Always return for example 23
GenerateAge(2); //Always return for example 34
GenerateAge(1); //Since the popSeed is also one also return 23
for(int i = 1; i<5 ; i++){
GenerateAge(i);
} // Always return for example 23,34,10,...
}
public int GenerateAge(int popSeed){
return unknownFunction(gameSeed,popSeed,1,99));
}
Edit 1: The reason i don't use System.Random Next() is because if i added and/or delete variable that use Next() then the result would change.
void GenerateHuman(int seed){
Random rnd = new Random(seed); //Say the seed are 1
Human i = new Human();
i.age = rnd.Next(1,99); //randomly return 36
i.strength = rnd.Next(1,20); //randomly return 4
}
//Say in few month i added new level var to the code
void GenerateHuman(int seed){
Random rnd = new Random(seed);
Human i = new Human();
i.age = rnd.Next(1,99); //still 36
i.level = rnd.Next(1,5); //randomly return 1
i.strength = rnd.Next(1,20); //not 4 but 19
}
//Then say i find the age not necresarry and delete it
void GenerateHuman(int seed){
Random rnd = new Random(seed);
Human i = new Human();
i.level = rnd.Next(1,5); //not 1 but 2
i.strength = rnd.Next(1,20); //not 19 but 4
}
Upvotes: 1
Views: 331
Reputation: 858
It seems you want to use your method like a static collection of numbers. You can generate new collection by changing the game seed.
The easiest way to accomplish your goal:
public int GenerateAge(int popSeed)
{
return new Random(gameSeed ^ popSeed).Next(1, 99);
}
Since Random class doesn't have method to reset its state, you need to create new object every time you need value. This situation may be unwanted.
The solution may be linear congruential generator:
public int GenerateAge(int popSeed)
{
long age = gameSeed;
for (int i = 0; i < popSeed; ++i)
{
age = (48271 * age) % int.MaxValue;
}
return (int)(1 + age % 99);
}
Upvotes: 1
Reputation: 29106
System.Random
has a constructor that takes a seed:
If you have two seeds, just combine them in whichever way you want:
var random = new Random(gameSeed ^ popSeed);
var n = random.Next(1, 99); // Will give a deterministic answer
Upvotes: 0
Reputation: 4218
If you have two seeds, you could combine them together in any number of ways to create one seed. As an example, you could use a Random
like so:
Random r = new Random(gameSeed ^ popSeed);
Next
is deterministic for a given seed, so just calling r.Next(1, 99)
should work fine.
Upvotes: 4