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 /
This question was closed Jun 15, 2013 at 11:59 AM by Eugenius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Eugenius · May 09, 2013 at 12:59 PM · collisionairandomrandom.range

How to exclude int values from Random.Range?

Hey,

I'm trying to have an Enemy move in random directions.

What I've done is set up 8 arrays of ints and for each value from the array I have a movement direction put in place.

My enemy will choose a random direction each time it hits one of the 4 walls (left,right,top,bottom). The problem is that if for the top wall I need for example ints 1,3,5 (which would be the accepted directions), how could I use Random.Range to make it select a value of either 1, 3 or 5 and never 2?

Regards, Eugen

Comment
Add comment · 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 Poranny · Aug 13, 2019 at 02:48 PM 0
Share

Hey!

I've created an asset designed exactly for that case (it's paid).

https://assetstore.unity.com/packages/tools/input-management/random-exclude-133786

Feel free to try it!

2 Replies

  • Sort: 
avatar image
13
Best Answer

Answer by whydoidoit · May 09, 2013 at 01:37 PM

    var validChoices = [1,3,5];

    function GetRandom() : int
    {
         return validChoices[Random.Range(0, validChoices.Length)];
    }
Comment
Add comment · Show 7 · 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 pad406 · May 09, 2013 at 03:52 PM 0
Share

Nice solution

avatar image Eugenius · May 09, 2013 at 04:27 PM 0
Share

That's great! $$anonymous$$arking it as an answer, although I'm receiving a Warning: Implicit downcast from 'object' to 'int'. Any way to get around it?

I have to mention that I did not use the code in the exact same way you have provided.

avatar image whydoidoit · May 09, 2013 at 04:47 PM 1
Share

Oh sure just make validChoices:

    var validChoices : int[] = [1,3,5];
avatar image Eric5h5 · May 09, 2013 at 04:58 PM 1
Share

That's not the cause of the warning. var validChoices = [1,3,5] already returns an int[] array (because it's an array containing only integers); the ": int[]" is superfluous. The code provided does not result in any warnings.

avatar image Eugenius · May 09, 2013 at 07:48 PM 0
Share

After making defining them as int[] it works perfectly. @Eric, as I mentioned my code was a bit modified.

Show more comments
avatar image
6

Answer by pad406 · May 09, 2013 at 01:43 PM

I think the simplest way is to create a function where you pass the direction that you don't want chosen and it then re-tries the random number if that is chosen.

eg.,

 int GetNewDir(int NotThisDirection)
 {
   int newDir;
 
   newDir = Random.Range(1,8);
   while (newDir == NotThisDirection)
   {
      newDir = Random.Range(1,8);
   }
 
   return newDir;
 
 }
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 Sarchophagi · Nov 23, 2017 at 01:59 AM 1
Share

Bad practice. In THEORY this while loop can take FOREVER and never randomize the expected numbers.

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

I need help with my enemy collision. 1 Answer

Make AI walk around randomly until Player is Seen. (C#) 2 Answers

Funny error. collision just hit 12 times 1 Answer

Movement around a huge object by avoiding obstacles 1 Answer

My question is almost fully answered(AI help) 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