- Home /
Problem in math or Random in flash export?
I have the same exact program running in Unity Desktop (Mac) export and Flash export. My program starts by creating random sets of parameters for 5 cats. The way this happens is a bit complex(its genetic modeling) but the key thing to know is that it uses Random.value and does math in uints that are then stored in bytes in a byte array.
On mac desktop i get a decent random selection, but on Flash they all turn out identical. There are no code differences between the two. Its the same exact unity project in both cases. The effect is shown below.
Help?
Desktop:
Flash:
Answer by Kryptos · Feb 06, 2012 at 10:57 PM
It is possible that the seed of the random generator is not properly initialized on Flash. Try to give a value to it.
// constant value should guarantee identical result in consecutive run.
Random.seed = 42;
Or
// try to get a random seed
yield return new WaitForSeconds( 1.111f );
Random.seed = (int) 100*Time.realtimeSinceStartup;
Another possibility lies with the way you stored your unints. Random.value returns a float between 0 and 1. Because you try to store it as uints in bytes array this may give different result according to Little Endian or Big Endian format.
Thabks for the suggestions.
The uints are just used as scratch variables for integer byte math to gaurd against over-flow. The end values are always 0-255.
But Ill look at the random and see if initializing it helps!
Then it is a good candidate for error with Little Endian and Big Endian format.
hmm. That would mean that casting in the flash is broken.
But Ill take a look at that specifically. Thanks
It is indeed the cast from float to byte that is failing.
But Convert.ToByte() comes up unresolved on export to Flash.
So how does one get a weighted average of two byte values as a byte value that works in Flash export?
If you give a seed the random values generated by Random.value will always be the same.
Answer by Jeff-Kesselman · Feb 07, 2012 at 06:29 AM
Apparently the conversion and/or the assignment operators are fairly broken.
I have gotten it to more or less work by using int instead of uint, minimizing the casting, and avoiding all assignment operators.
This is pretty awful though, some major work needs to go into assuring that math in C# gets correctly translated to the flash machine.
Answer by ABerlemont · Mar 09, 2015 at 10:27 AM
I just had this problem and fixed it by rewriting a random function
using UnityEngine;
using System.Collections;
public class RandomGenerator {
private static int _X = 0;
public static float value (){
if(_X == 0) set ();
_X = (_X * 123456789 + 987654321) % 100000;
return ((float)_X / 100000f * 0.5f + 0.5f);
}
public static float range (int max){ return value() * max; }
//public static void set (){ _X = System.DateTime.Now.Millisecond * System.DateTime.Now.Second; }
public static void set (){ _X = Mathf.FloorToInt(Time.realtimeSinceStartup * 10000); }
}
Note that System.DateTime is not available in flash ...
Your answer
![](https://koobas.hobune.stream/wayback/20220613063819im_/https://answers.unity.com/themes/thub/images/avi.jpg)