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 Baalhug · Jan 11, 2014 at 01:52 PM · randomnumberrepeat

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?

Comment
Add comment · Show 2
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 HappyMoo · Jan 11, 2014 at 02:25 PM 0
Share

Yes, that's odd. Code looks good. I would expect every number in the array to be the same

avatar image HappyMoo · Jan 11, 2014 at 02:38 PM 0
Share

Can you try System.Random? Just to see if that behaves as expected.

3 Replies

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

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.

Comment
Add comment · Show 1 · 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 Baalhug · Jan 11, 2014 at 03:45 PM 0
Share

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.

avatar image
1

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);
     }
 }


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

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

22 People are following this question.

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

Related Questions

audioclip repeat randomly -1 Answers

Random number different from previous generated. 6 Answers

Random.range multiple instantiations without repetition 1 Answer

Random gen script giving same numbers 3 times in a row 3 Answers

Quick SQLite question for Unity Game Database 1 Answer


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