• 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
Question by byurocks23 · Mar 19, 2014 at 07:59 PM · javascriptrandomnoobreturn

Random number that doesn't repeat. (javascript)

I'm not sure if its my logic that doesn't make sense or some syntax error, but it doesn't work. What I am trying to do is have a function that creates a random number every time you call the function. The only catch is three of the same numbers can't be created in a row. For example: 1,1,2 would work. 1,2,1,2,1,2,1,2 would also work. But 111 would not work. So if the number matches the previous two, it should choose another random number. Here is the errors I get:

Assets/NewBehaviourScript.js(13,10): BCW0023: WARNING: This method could return default value implicitly.

Assets/NewBehaviourScript.js(35,33): BCW0015: WARNING: Unreachable code detected.

Here is my current code:

 #pragma strict
 
 var firstNumber: int;
 var secondNumber: int;
 var thirdNumber: int;
 
 var firstTry: boolean = false;
 var secondTry: boolean = false;
 var thirdTry: boolean = false;
 
 
 
 function noRepeatRandom(){
     if (firstTry == false){
         firstNumber = Random.Range(1,4);
         firstTry = true;
         return firstNumber;
     }
     else if (firstTry == true && secondTry == false){
         secondNumber = Random.Range(1,4);
         if (secondNumber != firstNumber){
             firstTry = false;
         }
         else{
         secondTry = true;
         }
         return secondNumber;
     }
     else if (secondTry == true){
         while (true){
             thirdNumber = Random.Range(1,4);
             if (thirdNumber != secondNumber){
                 secondTry = false;
                 return thirdNumber;
                 break;
             }    
         }
     }
 }
 
 
 
 Debug.Log(noRepeatRandom());





I've used the return command in python but never in JavaScript, so those might be the problem.

Comment

People who like this

0 Show 1
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 Dblfstr · Mar 19, 2014 at 08:46 PM 0
Share

Added a code below. A little simpler than what you had, but it should do the job. There was no need for a return in the code, since the variable were being set within the function.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by byurocks23 · Mar 19, 2014 at 11:26 PM

I think I have found my solution

 var previousPreviousNumber: int = 0;
 var previousNumber: int = 0;
 var currentNumber: int = 0;
 var finalNumber: int;
 
 function noRepeatRandom(){
     while (true){
         currentNumber = Random.Range(1,4);
         if (currentNumber != previousNumber || currentNumber != previousPreviousNumber){
             finalNumber = currentNumber;
             previousPreviousNumber = previousNumber;
             previousNumber = currentNumber;
             break;
         }
     }
     Debug.Log("finalNumber " + finalNumber);
 }
 
 
 InvokeRepeating ("noRepeatRandom",2,1);

I've been running this for some time and I haven't encountered it running into three in a row.

Comment

People who like this

0 Show 0 · 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

Answer by Dblfstr · Mar 19, 2014 at 08:37 PM

 #pragma strict
      
     var firstNumber: int;
     var secondNumber: int;
     var thirdNumber: int;
      
     var differentNumber: boolean = false;
 
     function noRepeatRandom(){
 
     firstNumber = Random.Range(1,4);
     secondNumber = Random.range(1,4);
     thirdNumber = Random.range(1,4);
 
     while(!differentNumber)
     {
         if(secondNumber == thirdNumber){
             Debug.Log("Second and third number are the same")
             thirdNumber = Random.range(1,4);
         }
         else{
             Debug.Log("Second and third number are different")
             differentNumber = true;
         }
 
         }
     }
      
      Debug.Log("First Number :" +firstNumber);
      Debug.Log("Second Number :" +secondNumber);
      Debug.Log("Third Number :" +thirdNumber);
         
Comment

People who like this

0 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 byurocks23 · Mar 19, 2014 at 10:51 PM 0
Share

This works but the problem is that it creates three variables, each with different numbers. What I want is that each time the function is called, it creates one random number that cannot be equal to the two consecutive numbers before it.

avatar image Dblfstr · Mar 20, 2014 at 01:40 PM 0
Share

Yep, that is what this does,and is how I thought you wanted it to work. Sorry bout that.

avatar image

Answer by TykoX64 · Mar 19, 2014 at 08:39 PM

If you're using the function to set all three variables in 1 go as it looks like you shouldn't need to call return at all. just get the numbers, exit your while loop after the third number with break and be on your merry way :)

The problem your having here

Assets/NewBehaviourScript.js(35,33): BCW0015: WARNING: Unreachable code detected.

is that it gets to the first return and exits the function and will always exit the function there. return tells the function that you've done what you need to and to pass the variable after it to whatever called the function.

 function GetValue() : int
 {
     if (weWantValueX)
         return X;
     else
         return Y;
 }

This will test if weWantValueX and if so gives back X and ends the function, otherwise it continues and returns Y. if you have any other questions about returns just let me know.

Comment

People who like this

0 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 byurocks23 · Mar 19, 2014 at 11:02 PM 0
Share

I got my code error free and instead of using returns, I set the chosen value to a variable. The only problem is it seems it works for the first three numbers, but after that it sometimes has three in a row and sometimes four in a row. (It does seem less common though)

avatar image TykoX64 · Mar 20, 2014 at 02:01 PM 0
Share

what you want to do is set up an initializer function to create your first three numbers, then call a newNumber function to create just 1 new number.

EDIT: Apparently do while loops aren't supported in javascript... so here's the change.

 var firstNumber : int;
 var secondNumber : int;
 var thirdNumber : int;
 
 function SetUp()
 {
     firstNumber = Random.Range(1,4);
     secondNumber = firstNumber;
     while (secondNumber == firstNumber)
     {
         secondNumber = Random.Range(1,4);
     };

     thirdNumber = secondNumber;
     while (thirdNumber == secondNumber)
     {
         thirdNumber = Random.Range(1,4);
     };
 
 }
 
 function GetNext ()
 {
     firstNumber = secondNumber;
     secondNumber = thirdNumber;
 
     while (thirdNumber == secondNumber)
     {
         thirdNumber = Random.Range(1,4);
     };
 
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Random.range can only be called from main thread 0 Answers

Adding GUI to this 1 Answer

Instantiating a random dropped consumable item from many cloned objects 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