Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Sir Keyzinburga · Sep 06, 2011 at 08:18 AM · randomseed

several Random instances

Hello,

I would very much like to have 2 different Random instances, one which poops out real-random (or the unity approach to that) and one of which I can set the seed to a given Integer, making it poop out the same sequence every game cycle.

What i tried: searching on google ofcourse.. but it qutie hard to formulate a question that is not ambigious.

I tried instantiating a Random object by var rnd = new Random() but this is just a new pointer to the Random instance which unity provides. (probably because it is a singleton or something like that)

I hope this is clear enough.

As for sample code with which I tested the outcome:

 private var rand:Random;
 
 function Awake(){
     rand = new Random();
     rand.seed = 18427;
     //Debug.Log either rand.value or Random.value -> both will be the same
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by boat365 · Sep 06, 2011 at 08:30 AM

You could use the .NET framework System.Random instead; that will let you have distinct instances. It's still kind of a voodoo black box thing though, and you can't read the seed, or reseed it if you want to generate the exact same sequence of numbers again (for instance, if you want to save and reload the state of your random number generator).

Fortunately a linear congruential generator is not hard to implement yourself; it may be perfectly sufficient for throwaway random number generation that doesn't need to be particularly robust. See http://en.wikipedia.org/wiki/Linear_congruential_generator

edit: actually this is the wiki page I was thinking of- http://en.wikipedia.org/wiki/Lehmer_RNG

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 Sir Keyzinburga · Sep 06, 2011 at 08:42 AM 0
Share

I don't find myself able to understand it well enough to implement one myself, so I guess I will either have to learn it or find another solution.

avatar image boat365 · Sep 06, 2011 at 09:03 AM 1
Share

It looks complicated, but the Lehmer RNG is essentially one line of code- and it's even included in the page.

avatar image Sir Keyzinburga · Sep 06, 2011 at 10:14 AM 0
Share

Sorry, I have been busy trying to get it to work. But I can't get it to work in javascript, probably because I don't have enough +int stats.

What I can make out of it is the inital seed (a) is a number that is coprime to the modulo (n). I used the values given on the wiki, but I guess the problem lies in uint and ulongs, the syntax on the wiki ####UL is not recognized in javascript it seems. I tried to use floats ins$$anonymous$$d..

function lcg_rand(a:float):float { seed = (a * 279470273f) % 4294967291f; Debug.Log("seed: " + seed); return (seed/4294967291.0f); }

avatar image boat365 · Sep 06, 2011 at 10:41 AM 0
Share

I don't know about representing numbers in Unityscript; I use C# exclusively. Here's how it looks there:

class LCGRand
{
public static UInt32 Next (UInt32 a)
{
return (UInt32)(((UInt64)a * 279470273uL) % 4294967291uL);
}
}

Note that the value returned from this function is the seed that should be passed, unmodified, next time you call it (the first time you call it, you supply an initial seed).

If you wanted a random number between, say, 0 and 9 inclusive, you could use 'retval % 10'. But be sure to store the return value, and pass it unmodified to the next call.

Something like this:

// Print 5 random numbers between 0 and 9 inclusive
uint seed = 31;
for(int i = 0; i < 5; i++)
{
seed = LCGRand.Next(seed);
print(seed % 10);
}

avatar image Sir Keyzinburga · Sep 06, 2011 at 02:25 PM 0
Share

Thank you :D I am now able to get non-random random numbers :D Although I had to use UInt64 for all values otherwise it would return (mostly) negative values after the modulo modification.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 1 Answer

How do re-randomize Random.seed after setting it? 3 Answers

How to generate a random seed at a 24 hour interval 1 Answer

Can I disable randomization on particles? 1 Answer

How many many Pseudo-random sequences can be gotten from Random using Random.InitState()? 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