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
0
Question by byurocks23 · Mar 19, 2014 at 11:45 PM · javascripterrorrandomrange

can your Random.Range parameters match with function perameter (javascript)

So I have working code that picks a random number without having three duplicates. Here is an example below

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

I have other times I want to use this but I want a different range on the random number without having a separate function for each instance I need a different range. I then thought of using parameters like so:

 var previousPreviousNumber: int = 0;
 var previousNumber: int = 0;
 var currentNumber: int = 0;
 var finalNumber: int;
 
 function noRepeatRandom(min,max){
     while (true){
         currentNumber = Random.Range(min,max);
         if (currentNumber != previousNumber || currentNumber != previousPreviousNumber){
             finalNumber = currentNumber;
             previousPreviousNumber = previousNumber;
             previousNumber = currentNumber;
             break;
         }
     }
 }
 
 noRepeatRandom(1,10);

Except I get the error:

Assets/Scripts/Menu.js(97,45): BCE0004: Ambiguous reference 'Range': UnityEngine.Random.Range(int, int), UnityEngine.Random.Range(float, float).

Its not a big deal if I can't do this because I can just make more than one function, but according to my knowledge(witch is very limited) this should work.

Comment
Add comment
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

2 Replies

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

Answer by robertbu · Mar 20, 2014 at 12:08 AM

Try declaring your function like this:

  function noRepeatRandom(min : int, max : int)

Without specifying the type of the parameters, the compiler does not know which function you want to use to process the parameters.

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 supernat · Mar 20, 2014 at 12:05 AM

It doesn't work because you aren't defining the type for min and max (something JS lets you do for simplicity), but Random.Range needs to know if the min/max values passed in are floats or ints. For all it knows, when you call noRepeatRandom(1,10), you might expect a float back, because 1 and 10 are ways to represent floats (unfortunately). If you always plan to use ints or floats, just cast them. I can't say 100%, because I don't use JS, but you might get away with noRepeatRandom((int)1, (int) 10). But then the code starts getting ugly. Whether the compiler lets you do that I don't know. What I mean is it may still complain that it doesn't know what the type is at the function level.

By the way, if you ever need to get a random number within a range bigger than 3, this won't work. A more general solution is to start with a list of ints from min to max, and then get a random int from 0 to sizeof list. Then get the value at that index from the list and return it. Remove the value from the list, and the list size goes down by 1 for the next frame. When the list hits 0 size, re-create it by inserting min to max numbers in it again. This guarantees random values but never the same number twice in a row (except rare boundary conditions where lets say you pick the last value in the list (4) and the list is depleted, then you recreate the list, and the next frame you have an equal chance to pull 4 again. You can't really get around that easily though, unless maybe you use some kind of modulus algorithm maybe.

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 byurocks23 · Mar 20, 2014 at 12:38 AM 0
Share

declaring it worked, but the way @robertbu suggested it. And i've tested my function with a range of (1,7) and it seems to working as far as I can tell.

avatar image supernat · Mar 20, 2014 at 03:39 AM 0
Share

$$anonymous$$aybe I misinterpreted your need. I thought you wrote the function without specific types so you could call the function using floats or ints. :) With regard to repeating a number, is your intent to not have a number in the range repeat until all other numbers have been exhausted, or to only not repeat a number within the last 3 tries? If the latter, then yes your code is fine. $$anonymous$$y comment was in regard to the former. Glad you got it fixed though. :)

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

21 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

Related Questions

Random.Range statement question 1 Answer

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

Script not working (Java script) 2 Answers

Setting Scroll View Width GUILayout 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