- Home /
How to generate random number that will not be repeated?
I want to generate a random number several times, using the following function:
for (var i: int = 0; i <4; i + +)
{
randomPick = Random.Range (1,4);
}
But to each number only occur once. How to do it? Thanks in advance.
as I know what you need to do I do not know exactly how to do it. You will need a array filled with your numbers from 1-4 then run the random number to select from the array once you select it from the array you remove it from the array. Your random generator will need to be set on the high side at the array.length
Think of it like a deck of cards. First shuffle the deck, then remove one card at a time!
Look up list shuffling methods. Google is your friend here. Then, run a list of numbers that you want to randomise through the shuffle method, and then simply run through them one by one.
I swear on the first day of Comp Sci 101 there should be a massive red sign on the wall "not repeating is the same as shuffling, if you can't see this instantly try another career!" Heh!!
Answer by Bunny83 · Jun 09, 2013 at 06:12 AM
Just like that:
import System.Generic.Collections;
var randomNumbers = new List.<int>();
function Start()
{
for (var i = 0; i < 4; i++)
randomNumbers.Add(i);
}
function PickNumber() : int
{
if (randomNumbers.Count <= 0)
return -1; // No numbers left;
var index = Random.Range(0, randomNumbers.Count);
var value = randomNumbers[index];
randomNumbers.RemoveAt(index);
return value;
}
You can lose sleep finding the most elegant way to shuffle :O
for ( var i = 0; i < 4; i++ )
randomNumbers.Insert( Random.Range(0,i+1), i );
Answer by xdeath02 · Apr 03, 2017 at 06:49 AM
how to stop the repeating questions?? using this script??
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class txtcontrol : MonoBehaviour {
int range;
int oldRange;
int score;
public Text Question;
public Text Ans1;
public Text Ans2;
public Text Ans3;
public Text Ans4;
public Text FinalScore;
public Text ScoreText;
public Canvas Gamecan;
public Canvas Losecan;
public Canvas Wincan;
public Button Button_1;
public Button Button_2;
public Button Button_3;
public Button Button_4;
bool btnA;
bool btnB;
bool btnC;
bool btnD;
void Start () {
PlayerPrefs.SetInt ("Level2", 1);
SelectingRange ();
}
void Update () {
}
void SelectingRange()
{
Button_1.interactable = true;
Button_2.interactable = true;
Button_3.interactable = true;
Button_4.interactable = true;
Button_1.GetComponent<Image> ().color = Color.white;
Button_2.GetComponent<Image> ().color = Color.white;
Button_3.GetComponent<Image> ().color = Color.white;
Button_4.GetComponent<Image> ().color = Color.white;
oldRange = range;
range = Random.Range (1, 6);
if (oldRange == range)
{
SelectingRange();
}
else
{
SpawnQuestion ();
}
}
void SpawnQuestion()
{
if(range == 1)
{
Question.text = "1?";
Ans1.text = "oo";
Ans2.text = "yes";
Ans3.text = "maybe";
Ans4.text = "yup";
btnA = true;
}
if(range == 2)
{
Question.text = "2?";
Ans1.text = "oo";
Ans2.text = "yes";
Ans3.text = "maybe";
Ans4.text = "yup";
btnB = true;
}
if(range == 3)
{
Question.text = "3!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 4)
{
Question.text = "4!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 5)
{
Question.text = "5!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 6)
{
Question.text = "6!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 7)
{
Question.text = "7!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 8)
{
Question.text = "8!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 9)
{
Question.text = "10!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 10)
{
Question.text = "10!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 11)
{
Question.text = "11!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 12)
{
Question.text = "12!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 13)
{
Question.text = "13!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 14)
{
Question.text = "14!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 15)
{
Question.text = "15!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
if(range == 16)
{
Question.text = "16!?";
Ans1.text = "hello";
Ans2.text = "hi";
Ans3.text = "hehe";
Ans4.text = "yup";
btnD = true;
}
}
public void ButtonA()
{
DisacttiveButtons ();
if (btnA == true)
{
Button_1.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnA = false;
AddScore ();
}
else
{
Button_1.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
public void ButtonB()
{
DisacttiveButtons ();
if (btnB == true)
{
Button_2.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnB = false;
AddScore ();
}
else
{
Button_2.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
public void ButtonC()
{
DisacttiveButtons ();
if (btnC == true)
{
Button_3.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnC = false;
AddScore ();
}
else
{
Button_3.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
public void ButtonD()
{
DisacttiveButtons ();
if (btnD == true)
{
Button_4.GetComponent<Image> ().color = Color.green;
Invoke ("SelectingRange", 1);
btnD = false;
AddScore ();
}
else
{
Button_4.GetComponent<Image> ().color = Color.red;
Invoke ("YouLost", 2);
}
}
void AddScore()
{
score = score + 1;
ScoreText.text = "Score: " + score;
if (score == 15)
{
Invoke ("YouWon", 1);
}
}
void YouLost()
{
Gamecan.gameObject.SetActive (false);
Losecan.gameObject.SetActive (true);
FinalScore.text = "You have correctly answered:" + score + " question!";
}
void YouWon()
{
Gamecan.gameObject.SetActive (false);
Wincan.gameObject.SetActive (true);
}
void DisacttiveButtons()
{
Button_1.interactable = false;
Button_2.interactable = false;
Button_3.interactable = false;
Button_4.interactable = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613103820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random.seed Repeating 2 Answers
Create number of enemies when one is deid 1 Answer
¿how to take all values from an array without order? 2 Answers
Using arrays to randomly select an object 0 Answers
Random Range Seems... Unrandom 1 Answer