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 samir_kouider · Apr 03, 2014 at 10:58 PM · arrayrandommultiplestorerange

Store multiple random integers in an array?

Hi, all i currently have a random number generating between 1,5 using random.Range. And this integer is only ever randomised 4 times.

My problem is with each of the integers being created i cant store them individually to an array as it just seems to update the entire array to the new number which is created?

 //randomFace, rnadomNumbers and result are integers
 //this code block is in the update method

                int[] randomNumbers = new int[4];

                 while(randomFace == result)
             {
                 randomFace = Random.Range(1,5);
             }
             result = randomFace;
             for(int i = 0; i < randomNumbers.Length; i++)
             {
             randomNumbers[i] = randomFace;
             }
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 Scribe · Apr 04, 2014 at 12:10 AM 0
Share

think through it carefully, say we start with result equal to 3 for example, then while randomFace is equal to 3 we set it to a random number maybe it becomes set as 4... then the while loop ends and we set the result to 4. now randomFace is still only one integer -> 4 and for every integer in our integer list (randomNumbers) we set them all to randomFace which is always equal to 4.

If you could expand on your explanation of "each of the integers being created i cant store them individually to an array" I may be able to help, are you trying to get 4 random numbers that do not include the previous result value?

Scribe

avatar image samir_kouider · Apr 04, 2014 at 10:04 AM 0
Share

Yes. Exactly. Randomise once store the value in the first slot of the array. Randomise value again store the value in the second slot of the array and so on. So within the array you will have different numbers

4 Replies

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

Answer by samir_kouider · Apr 04, 2014 at 03:18 PM

I have found the answer. In c# you must use a list and just add to the list

 //add this line to top
 using System.Collections.Generic;
 
 
 public List<string> randomNumbersArray;
 public int maxArrayLength = 4;
 
 void Start()
 {
     randomNumbersArray = new List<string>();
 }
 void Update()
 {
      while (randomFace == result){ 
          randomFace = Random.Range (1, 5);
       }
     if(randomNumbersArray.Count < maxArrayLength)
     {    
       randomNumbersArray.Add(randomFace.ToString());
      }
 }
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
1

Answer by Sisso · Apr 03, 2014 at 11:00 PM

http://answers.unity3d.com/questions/162579/how-to-debug-unity-script.html

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 c4_ · Apr 04, 2014 at 12:40 AM

In my very limited experience, if you put a Random function in the Update() class it changes every time Update() updates. So in your case, each Random is changing every time you call a new Random.

I've only had success getting Random functions to work in Start(), Awake(), or Update(). Start() seems to be the preferred choice of Unity documentation examples, Update() is NOT my preferred destination.

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 supernat · Apr 04, 2014 at 03:16 PM 0
Share

Technically, once you set the Random.seed value, every call to Random.Range returns the next pseudorandom value from the internal array starting at that seed. This behavior is not dependent on where the method is called. However, setting the seed is only useful to generate large "random" data that is predictable. You could for instance set the seed to 1000 in a large space game to regenerate the same predictable universe components without storing the entire contents of the universe.

avatar image
0

Answer by ransomink · Apr 04, 2014 at 05:31 AM

Because your while loop only creates a random integer once (then breaks), during your for loop, it's setting every integer in your array (randomNumbers) to the same value: randomFace.

 // if randomFace is equal to the result
 while ( randomFace (3) equals result (3) )
 {
     // set randomFace to a random integer. This will break the loop
     randomFace (3) now equals random integer (4)
 }
 
 // result becomes the 'new' randomFace integer
 result (3) now equals randomFace (4)
 
 // iterate through every value inside the randomNumbers array
 for ( iterate through every value inside the array )
 {
     // 4 values are listed because you stated there were 4 values inside the randomNumbers array
     array value [0] = randomFace (4)
     array value [1] = randomFace (4)
     array value [2] = randomFace (4)
     array value [3] = randomFace (4)
 }

If you're only going to have 4 — or a set amount of — random integers, you should create an empty array; Check if the length is less than 4, if so, then generate a random integer and add it to the array. The code will continue until there are 4 random integers inside of your array.

In order to do this, you must use the normal Javascript Arrays '()' and not the built-in arrays '[]' because their size cannot be changed. You can easily copy a normal array into a builtin array so you can see the random numbers generate inside of the inspector.

 //////////////////////////////////////////////////
 // STORE RANDOM INTEGER IN ARRAY
 //////////////////////////////////////////////////
 
 // INSPECTOR VARIABLES //
 public var delayTime : float;// wait time before storing a new random integer (for the purpose of this question)
 public var range : Vector2;// the minimum and maximum range to use when creating a random number (in your case: 1,5)
 public var randomInt : int;// the current random integer to be stored inside of the array (I made it public so you can see what random integer was generated. If you use the built-in array method then you can keep this variable private.
 public var maxArrayLength : int;// the maximum length allowed for the array
 public var randomIntArray : Array;// an array holding the value of each random integer
 public var builtinRandomIntArray : int [];// a builtin array used to show the current values of the randomIntArray inside the inspector
 
 // PRIVATE VARIABLES //
 private var resetDelayTime : float;// reference to the delayTime variable
 
 // used for initialization
 function Start ()
 {
     randomIntArray = new Array ();// create an instance of the array
     resetDelayTime = delayTime;// set resetDelayTime to the current delayTime
 }
 
 // update is called every frame
 function Update ()
 {
     delayTime -= Time.deltaTime;// start the countdown. Subtract delayTime by each second (based on last frame)
     
     // if the length of the array is shorter than the maximum allowed
     if ( randomIntArray.length < maxArrayLength )
     {
         // ... and if the wait period is over
         if ( delayTime <= 0 )
         {
             // ... create a random integer and add it to the array (holding all the random integers)
             delayTime = 0;// set the delayTime to 0 to stop it from counting down
             randomInt = Random.Range ( range.x, range.y );// create the random integer using the values from range
             randomIntArray.Push ( randomInt );// add the random integer to the (randomIntArray) array
             builtinRandomIntArray = randomIntArray.ToBuiltin ( int ) as int [];// copy the javascript array into a builtin array (so you can see the random integers inside the inspector)
             Debug.Log ( randomIntArray );// display the current values of the randomIntArray to the console
             
             delayTime = resetDelayTime;// reset the delayTime to restart the countdown (the countdown is used to insert a random integer inside of an array. Used for testing purposes only)
         }
     }
     else
     {
         Debug.LogWarning ( "Limit has been rached!" );
         delayTime = 0;// set the delayTime to 0 to stop it from counting down
         return;
     }
 }

I created a test showing how this could be implemented. Hope this helps...

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 samir_kouider · Apr 04, 2014 at 02:26 PM 0
Share

thanks for this although i'm using c# and array.push does not exist

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

25 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 avatar image avatar image avatar image

Related Questions

Record multiple AudioClip one after one and store then into an array. 1 Answer

Choose random Gameobject from array? 4 Answers

renderer.material doesnt work 3 Answers

How to know what random number is chosen 2 Answers

Random Range Seems... Unrandom 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