- Home /
How to repeat a random result
Hi, guys:
Apparently I have the opposite problem to everyone else: I want to repeat a random result.
My code is this:
void random_gen(){
for (int i=0;i<4000;i++){
Random.seed=3;
int posRand = Random.Range(0,2000);
array1[i]=posRand;
}
}
But array1 gets different values all the time. I dont want to generate 4000 numbers by hand and write it in script definitions, but i want the same array result in every run of the game. Do you figure out something?
Yes, that's odd. Code looks good. I would expect every number in the array to be the same
Can you try System.Random? Just to see if that behaves as expected.
Answer by Nickpips · Jan 11, 2014 at 02:10 PM
You are changing the seed every loop. Take Random.seed=3; and pull it out of the loop, and make 3 a bigger number, just put a constant in it (Not too large though). Another option is a custom number generator. Take this for example:
void random_gen() {
for( int i = 0; i < 4000; i++ ) {
uint Seed = (uint)(bignum + bignum*i);
Seed = (uint)(Mathf.Abs((69621*Seed)% 2000 ));
array[i] = (int)Seed;
}
}
bignum is just your seed, which can be an arbitrary number.
If i do:
void random_gen(){Random.seed=1212;for (int i=0;i<4000;i++){int posRand = Random.Range(0,2000);array1[i]=posRand;}}
the result is the same (different arrays every call).
Your random generator is kinda cool, but it has the same result as:
void random_gen() {
for( int i = 1; i <= 4000; i++ ) {
uint Seed = (uint)bignum*i;
Seed = (uint)(69621*Seed)% 2000;
array[i] = (int)Seed;
}
}
And actually bignum is not needed, as the result will always be a number between 0 and 2000. The only problem is 69621 may be too litle in comparison to 2000, so you can add 2 or 3 more digits and it's done.
I will use this number generator for my code as it fits my purposes but i will keep the question open to see if anyone has the key for the random function and the seed behaviour.
Answer by KellyThomas · Jan 11, 2014 at 04:01 PM
You can always use the .NET/Mono Random class.
using System.Random;
// ...
void random_gen() {
for (int i=0; i < 4000; i++) {
System.Random random = new System.Random(3);
array1[i] = random.Next(2000);
}
}
Answer by Baalhug · Jan 11, 2014 at 03:55 PM
Sorry, i forgot to update the script (i had it copied). My code works with seed inside and outside the loop.
Your answer