- Home /
How to make array to non-same random value?
using UnityEngine; using System.Collections;
public class Apple : MonoBehaviour {
public int[] a;
void Start () {
a = new int[4];
Shuffle ();
}
public void Shuffle(){
for (int i = 0; i < a.Length; i++) {
a [i] = Random.Range (0, 20);
Debug.Log (a [i]);
}
}
Result > 0,9,18,9 .. '9' is Same. i dont want that value. How to make array to non-same random value?
}
Answer by EmmaEwert · Dec 27, 2014 at 01:23 PM
Generate a list of integers between your desired min and max values.
Extract the amount of elements you need, updating the list.
Back-of-envelope suggestion:
List<int> ints = new List<int>();
List<int> values = new List<int>();
int min = 0;
int max = 20;
int needed = 5;
for (int i = min; i <= max; ++i) {
ints.Add(i);
}
for (int i = 0; i < needed; ++i) {
int index = Random.Range(0, ints.Count);
values.Add(ints[index]);
ints.RemoveAt(index);
}
Note that this solution does no bounds checking, and will throw an exception when you request more values than exist between max
and min
.
I LOVE YOU!! im find good way!!! Oh my God! very very Thanks!!!!! I think I've found the gold Two friends ver ver very! thank!! so much!!
It should be noted that the answer by Pkninja16 is asymptotically slower; O(∞), in fact (as the Random#Range call is not guaranteed to ever return an acceptable candidate and as such cannot guarantee anything about the required repetitions, or even whether the while
loop will ter$$anonymous$$ate at all).
The solution I have proposed is not the most performant, but for sufficiently small ranges of possible values, it should be performant enough. It has a time complexity of about O(n+m), where n = max - $$anonymous$$
and m = needed
- depending on the implementation of List#Add, List#[] and List#RemoteAt, of course.
For the approach above to be repeatedly used in practice, you probably ought to think about what should happen when you ask for more elements than there are possible values. I have edited the original answer to clarify the lack of bounds checking.
Answer by Pkninja16 · Dec 27, 2014 at 10:04 AM
You can check if the number that you generated is already in the array by using the IndexOf(int) method.
for (int i = 0; i < a.Length; i++) {
int random = Random.Range(0,20);
while(a.IndexOf(random)> -1)
{
random = Random.Range(0,20);//This will keep on generating a number until there is no repeat
}
a [i] = random;
Debug.Log (a [i]);
}
Now be weary that if the array is longer, the values from 0-20 may be all taken up so the while loop will keep running forever because there is no way for it to make a number that isn't already in the array. Hope this helps!
VeryVeryVery Thank! but .. error message .. i'm Crying T_T ..
error CS1501: No overload for method IndexOf' takes
1' arguments
IndexOf method is died. why? Please one more answer.
No good answer, you have a while loop, which means your game is about to stop until the right value is found. If you need 10 values out of 20 possible ones, as you go on, you are more and more likely to hit a miss, by the time you look for the 10th value, you have 50% chances to never find an unused value. Wrong.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pick between two floats 2 Answers
C# array equal to another array minus one entry. 1 Answer
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
Accessing Instance Bool from other script 2 Answers
Multiple Cars not working 1 Answer