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 quantumface · Jun 16, 2015 at 02:29 AM · randomexceptionrangenumber

Random number with exceptions?

Hello. Is there a way, for example, to generate a number between 0 and 9, without the chance to roll 3? Can I add an exception for the 3? Would the best way be just adding the range 0-9 but re-doing the function whenever the outcome is 3?

Or if I have several exceptions, 2, 5 and 7? The function re-flowing chance increases as more exceptions appear. Imagine if I need a number between 0-999 except 800 non-uniform ones. Is there such an implementation or do I need to get around it by hard-coding?


ON A SIDE NOTE: I'm just working for an MMORPG company in a parallel universe, where this pimple-faced loser teenager gets a new Mighty Axe with 1250-1600 damage and is about to hit seven 1337's in a row. I know he won't notice it while sipping on his parent-provided sugary drink with electrolytes, but I still don't want him the blessing of luck. I have all the time in the world(s), because when time is bent - so is the sanity.

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 Eno-Khaon · Jun 16, 2015 at 02:46 AM

Looking around a bit, there doesn't appear to be any specific means of doing that. I think you would probably just want to assemble a List<> of any values you don't want included in the number generation, then consult that List<> after number generation to determine whether it should be excluded (and, therefore, generate a new number in its place).

Edit: Working around HTML issues in the post

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

Answer by zach-r-d · Jun 16, 2015 at 03:00 AM

Simply re-rolling is highly inadvisible, especially with lots of exceptions; it can lead to large, random performance drops or stack overflows, and in theory, the function could never return.

One easy way implement to implement this is to create a list, fill it with the full range of numbers in consideration, then remove any disallowed numbers from the list. After that, it's just a matter of selecting a random element from the list. This also happens to be the best way to handle large numbers of exceptions.

If the range of numbers is large and there are few exceptions, there is a better way to do it, but I'd still recommend trying the list first.

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

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

Random.Range statement question 1 Answer

Randomly generated number 0 Answers

Random.Range(..) not working 1 Answer

Various of Random Numbers? 1 Answer

ArgumentException: RandomRangeInt can only be called from the main thread. 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