Not repeat random number (Random.Range)
Hello, how can I prevent from a same random number? thanks :)
By definition a random number is completely random, therefore duplicates can occur. If you really can't use the same number, then you need to keep a list of numbers you've seen. If you generate a random number that you've already seen, generate a new random number
Are you talking about duplicates in one sequence or a completely identical sequence of random numbers? The first case has been answered.. the second involves the term seed, which you can resesarch
Quick reference for people looking for a number of iterations inferior of equal to the range size: removal and shuffling methods in duplicate http://answers.unity3d.com/questions/471420/how-to-generate-random-number-that-will-not-be-rep.html
For more iterations (show RPG damages in chain), the removal + array reload method below should work.
However, in your case I imagine that you locate damage labels with Vector2 / floats, which is more complicated because you would need to exclude 2D regions to apply removal. http://answers.unity3d.com/questions/354765/how-to-get-non-repeating-randomrange.html show how to place an object on a 2D plane, by either ensuring the distance to previous objects is big enough (risk of repeated iterations) or dividing the space in a discrete grid (so that discrete methods work).
Answer by karl_ · Jul 22, 2013 at 10:18 PM
You could store the used values in a list or array, then check against it before using the random value.
Ex:
List<int> usedValues = new List<int>();
public int UniqueRandomInt(int min, int max)
{
int val = Random.Range(min, max);
while(usedValues.Contains(val))
{
val = Random.Range(min, max);
}
return val;
}
Sometimes I get repeating numbers
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))
{
for (int i = 0; i < 3; i++)
{
Debug.Log(UniqueRandomInt(-5, 2));
}
}
}
Answer by fafase · Mar 05, 2015 at 07:07 AM
In order to never get twice the same value, you need to first store them and remove as you get them:
int [] array = new int[]{0,1,2,3,4,5,6};
List<int>list = null;
void Start(){
list.AddRange(array);
}
int GetUniqueRandom(bool reloadEmptyList){
if(list.Count == 0 ){
if(reloadEmptyList){
list.AddRange(array);
}
else{
return -1; // here is up to you
}
}
int rand = Random.Range(0, list.Count);
int value = list[rand];
list.RemoveAt(rand);
return value;
}
@fafase the code is working the only problem is my scene is still repeated. can you help me fix my code?
for a solution in finite time for a discrete space, and allowing reload.
Note that Random.Range is inclusive according to Unit API, so you should use list.Count - 1 (not verified myself).
If the array is small or you know that you will use most of the values in the array, a shuffle (creating an array with reorderer values at once) is also acceptable. The "reload" part would be done by shuffling the array again.
what if last number in the list gets the same as first number in the next loaded list.....
Answer by Romejanic · Jan 16, 2015 at 10:53 AM
private static int lastRandomNumber;
public static int generateRandomNumber(int min, int max) {
int result = Random.Range(min, max);
if(result == lastRandomNumber) {
return generateRandomNumber(min, max);
}
lastRandomNumber = result;
return result;
}
Hope this helped you, Romejanic
Answer by fransh · Jul 22, 2013 at 10:17 PM
var newRandomNummer : float;
var lastRandomNumber : float;
var min : float;
var max : float;
function MakeRandomNumber() {
newRandomNummer = Random.Range(min,max);
if(newRandomNummer != lastRandomNumber){
print(newRandomNummer);
}
else{
//retry!
}
}
That could work.
I want to make a combat system in my mmorpg game. $$anonymous$$y problem is when I hit 2 or more enemys I get their life + dealed damage not in random locations but in the same locations so I can't see their life and the dealed damage.. any suggestions?
Wrong way of doing here.
Here, each round you have a probability of missed hit, for instance, if you have only 2 values, you have a 50% chance to enter an endless loop and stop your game until you get the right value.
Answer by Eydamson · Mar 05, 2015 at 07:03 AM
you could use recursive functuion try this
float RandomNum(float lastRandNum)
{
float randNum = Random.Range(min, max);
if (randNum == lastRandNum)
{
return RandomNUm(randNum);
}
else
{
return randNum;
}
}
In anything except Lisp or $$anonymous$$L, we just turn tail recursion into a loop: while(randNum==lastRandNum).
Plus, the poor OP is so confused they can't even describe the problem. $$anonymous$$entioning recursion just seems cruel.
Your answer
Follow this Question
Related Questions
How to generate random values with a uniform distibution 0 Answers
Random.range game object destroying itself before reaching destroy position.Please help. 1 Answer
Is there a way to use Random.Range without inheriting from Monobehaviour? 2 Answers
How to generate a random color? 5 Answers
Random Generation problem GameObject 0 Answers