Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Giannis7312777 · Jul 22, 2013 at 09:55 PM · random.range

Not repeat random number (Random.Range)

Hello, how can I prevent from a same random number? thanks :)

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 perchik · Jul 22, 2013 at 10:15 PM 1
Share

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

avatar image Jamora · Jul 22, 2013 at 10:35 PM 1
Share

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

avatar image huulong · Nov 07, 2016 at 12:53 PM 0
Share

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).

7 Replies

· Add your reply
  • Sort: 
avatar image
3

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;
 }
Comment
Add comment · Show 1 · 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 CCV334 · Jul 31, 2017 at 05:48 PM 0
Share

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));
             }
         }
     }
avatar image
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;
 }

Comment
Add comment · Show 3 · 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 glady · Aug 15, 2016 at 03:43 PM 0
Share

@fafase the code is working the only problem is my scene is still repeated. can you help me fix my code?

avatar image huulong · Nov 07, 2016 at 01:00 PM 0
Share
  • 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.

avatar image DurgeshV · May 12, 2017 at 09:43 AM 0
Share

what if last number in the list gets the same as first number in the next loaded list.....

avatar image
1

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

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
avatar image
0

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.

Comment
Add comment · Show 2 · 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 Giannis7312777 · Jul 23, 2013 at 07:52 AM 0
Share

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?

avatar image fafase · Mar 05, 2015 at 06:58 AM 0
Share

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.

avatar image
0

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;
         }
     }
Comment
Add comment · Show 1 · 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 Owen-Reynolds · Aug 15, 2016 at 04:03 PM 0
Share

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.

  • 1
  • 2
  • ›

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random.Range Issues [Java] 1 Answer

Random Range function changed? 2 Answers

Random.Range gives the same value in WebGL Build 0 Answers

Instantiating random prefabs gives wrong position 3 Answers

Random generated rooms in network multiplayer,Randomly generated rooms in mulitplayer 0 Answers


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