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
1
Question by Tyler 2 · Dec 26, 2010 at 02:33 AM · randomifcondition

random range/if conditions?

Hello. How can I use Random.Range(#,#) in combination with an if statement? So I can do something like

(Random.Range(1, 10); if =1 {something} if =2 {something else} if =2 {something else} ...

Thanks

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
0
Best Answer

Answer by ina · Dec 26, 2010 at 03:00 AM

Random.Range(a,b) returns a number between the two endpoint numbers a (inclusive) and b, (exclusive). This means, it will return anything between a, a+1, a+2 ... b-2, b-1

To use it, you will want to assign a variable var rand:int=Random.Range(1,10);, and then, compare it to the various possibilities in your if then. if(rand==1)doSomething(); else if (rand==2)doAnotherThing(); etc.

You may also wish to use if(rand>1) and comparison operators for entire ranges of numbers.

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 Mike 3 · Dec 26, 2010 at 08:38 AM 0
Share

note - the int version of Random.Range is not inclusive. Random.Range(0, 10) will return a maximum of 9

avatar image ina · Dec 26, 2010 at 10:00 AM 0
Share

mea culpa!! and wow - why is the second endpoint for int exclusive? that seems so random

avatar image Jesse Anders · Dec 26, 2010 at 06:34 PM 1
Share

Ranges in languages such as C#, C++, etc. are often represented as [begin, end), with the last element excluded. This is consistent with the use of zero-based indexing, where the index 0 is valid and the index equal to the size of the array is invalid (one past the end). From this point of view, the behavior of the 'int' version of Random.Range() makes sense (for example, it can be used in a straightforward manner to select an element of an array randomly).

avatar image
1

Answer by SarperS · Dec 26, 2010 at 04:06 AM

You can also use a switch statement.

var randomNumber:int = Random.Range(0, 2);

switch(randomNumber){

case 0: //Do this, do that break;

case 1: //Do this, do that break;

case 2: //Do this, do that break; }

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

No one has followed this question yet.

Related Questions

How to optimize if statements - can't remember term 1 Answer

If statement question... 1 Answer

Random value for if statement 2 Answers

if condition question... 1 Answer

GameObject tag to if condition 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