Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by rottweiler · Jun 09, 2013 at 06:04 AM · random

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bodec · Jun 09, 2013 at 06:08 AM 0
Share

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

avatar image syclamoth · Jun 09, 2013 at 06:09 AM 1
Share

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.

avatar image Fattie · Jun 09, 2013 at 06:56 AM 0
Share

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!!

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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;
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rottweiler · Jun 09, 2013 at 06:30 AM 0
Share

Yes, it works. Thanks.

avatar image Fattie · Jun 09, 2013 at 06:55 AM 0
Share

Surely .Net has a shuffle ?

avatar image rottweiler · Jun 09, 2013 at 10:59 AM 0
Share

I think it has)

avatar image Fattie · Aug 18, 2013 at 02:20 PM 1
Share

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 );
avatar image creamywhite · Aug 16, 2016 at 08:18 AM 0
Share

where will I attach this script? Thanks.

Show more comments
avatar image
0

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;
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges