Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jeff-Kesselman · Feb 06, 2012 at 08:52 PM · randommathflash

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:

alt text

Flash:

alt text

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Feb 06, 2012 at 08:56 PM 0
Share

$$anonymous$$ac:

alt text

Flash:

alt text

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Feb 06, 2012 at 11:00 PM 0
Share

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!

avatar image Kryptos Jeff-Kesselman · Feb 06, 2012 at 11:03 PM 0
Share

Then it is a good candidate for error with Little Endian and Big Endian format.

avatar image Jeff-Kesselman · Feb 06, 2012 at 11:07 PM 0
Share

hmm. That would mean that casting in the flash is broken.

But Ill take a look at that specifically. Thanks

avatar image Jeff-Kesselman · Feb 06, 2012 at 11:52 PM 0
Share

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?

avatar image ABerlemont · Mar 09, 2015 at 09:44 AM 0
Share

If you give a seed the random values generated by Random.value will always be the same.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Algorithm for number generation that is random but consistent 2 Answers

Random InitState not working with Coroutines / IEnumerator? 2 Answers

Having random operators in an equation? 1 Answer

Normal distribution random 3 Answers

Math question about random range and fractions 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges