- Home /
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;
}
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
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
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());
}
}
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.
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.
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...
thanks for this although i'm using c# and array.push does not exist
Your answer
Follow this Question
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