- Home /
Two different random numbers
Hello every one
I want to have 2 different random numbers in other word I was wrote this code for my game that makes different places and never shouldn't chosen prefab be same as chosen position. I don't how to do it?
I think one of them could be random and other one be serial and then that one random shouldn't be repeated and also the chosen prefab shouldn't be same with chosen position.
function Replace (){
var PrefabsList : List.<GameObject> = new List.<GameObject>(prefabs);
var PositionsList : List.<Vector3> = new List.<Vector3>(positions);
while(PrefabsList.Count > 0){
var ChosenPosition : int = Random.Range(0 , PositionsList.Count);
var ChosenPrefab : int = Random.Range(0 , PrefabsList.Count);
PrefabsList[ChosenPrefab].transform.position = PositionsList[ChosenPosition];
PrefabsList.RemoveAt(ChosenPrefab);
PositionsList.RemoveAt(ChosenPosition);
}
GameObject.Instantiate( chief_Prefab , twelve_InGame.transform.position , Quaternion.identity);
Materials.CheckMat = true;
}
Please answer code with Unity JS
thanks in advance
Answer by InsertFunnyUsernameHere · May 29, 2015 at 08:20 AM
So basicaly you want ChosenPosition to be different than ChosenPrefab? If so try this:
While(ChosenPosition==ChosenPrefab)
{
ChosenPrefab=Random.Range(0,PrefabList.Count);
}
thanks , but if you look at that code it removes the chosen ones from array list and that will hang on because both of them will be same number at the last one and it's 1 or 0 -_- I think should add a simple if(list.count > 2) and then your code with while and ...
Okay then, modify this slightly
While(ChosenPosition==ChosenPrefab&&PrefabsList.Count>1&&PositionsList.Count>1)
{
ChosenPrefab=Random.Range(0,PrefabList.Count);
}
Answer by yebai · Nov 29, 2017 at 03:41 AM
A better way without using while() or other loops
var ChosenPosition : int = Random.Range(0 , PositionsList.Count);
var ChosenPrefab : int = (ChosenPosition + Random.Range(1 , PrefabsList.Count )) % PositionsList.Count;
Your answer
Follow this Question
Related Questions
How do Ensure that a series of int's are never the same? 1 Answer
How to create random movement in 2D 2 Answers
Random.seed on different hardware? 1 Answer
Disreguard random number after use... 3 Answers
How to use random numbers? 1 Answer