Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 numbus · Aug 21, 2017 at 10:03 PM · c#randommathrandom.rangerandomize

How to make Random.Range() step by a certain amount?

I doubt that the title alone is enough information to understand the question I'm asking, so I'll ask by example:

In my game my enemies have a certain minSpawnHealth and maxSpawnHealth variable which determines the current health of my enemy on spawn. This way when the enemies spawn they can have a randomized amount of health between the min and the max. In order to find that randomized health I'm using "Random.Range(minSpawnHealth, maxSpawnHealth+1)", which is pretty standard.

However, I want the returned value of Random.Range() to step by a certain amount, say 5 or 10.

Let's say that minSpawnHealth is 10 and maxSpawnHealth is 30, so that a new enemy could spawn somewhere between 10-30 health. I don't want my enemy to have the possibility of spawning with 13 health, or 17 health, or 26 health, etc. I would want my enemy to spawn with health by intervals of 5 (10, 15, 20, 25, 30 health) or 10 (10, 20, 30 health), or whatever arbitrary step amount I would want Random.Range() to return.

Is there any inherent way to do this (which I haven't found yet) or do I just have to be clever with math adjustments after the randomized value is created?

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

1 Reply

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

Answer by pslattery · Aug 21, 2017 at 10:28 PM

I think the simplest way to do this would be:

 int minSpawnHealth = 10;
 int maxSpawnHealth = 30;
 int stepSize = 5;
 
 int GetRandomHealth () {
     int randomHealth = Random.Range(minSpawnHealth, maxSpawnHealth);
     int numSteps = Mathf.Floor (randomHealth / stepSize);
     int adjustedHealth = numSteps * stepSize;
 
     return adjustedHealth;
 }
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 numbus · Aug 21, 2017 at 10:42 PM 0
Share

Works very well and can be applied outside of this question scope, thank you!

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

73 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Non-repeating random number generator crashing unity 1 Answer

Need help with generating random Multiplier,Need help with generating random multipliers 0 Answers

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

How can I randomly create a number and then delete it to stop repeating 3 Answers

Distribute terrain in zones 3 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