• 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
Question by superventure · May 26, 2011 at 06:53 AM · randommathrangedivide

Math question about random range and fractions

How do you say 2/3 of a number to give in random range? This is what I tried and it seemed to work at first, but this is a no go.

 var  power = //this is an inspector input number
 Slam = Random.Range(power/3 + power/3, power +1);


I am basically trying to say that I want slam to be a random number between 2/3s of power and total power.

Thanks

Comment

People who like this

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

Answer by CHPedersen · May 26, 2011 at 07:05 AM

I just tested this (in C#), it worked fine for me and seems to do what you want:

 int test = 5;
 float Slam = Random.Range(0.6666f * test, test);

The reason it didn't work for you is possible due to your division getting truncated to an integer, if power is dynamically typed to an integer.

Notice that,

 int test = 5;
 Debug.Log(test / 3);

Outputs "1", because (int)(5/3) = 1.

Comment
hellcats

People who like this

1 Show 6 · 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 superventure · May 26, 2011 at 07:12 AM 0
Share

What about in Js? I tried .6666 and it's still giving me weird results.

avatar image CHPedersen · May 26, 2011 at 07:21 AM 0
Share

Can you elaborate on "weird results"? Try Debug.Log'ing both the precise input and the results, to see what is actually being produced. If you have 0.6666f*power taking place inside the call to Random.Range, then move it out and Debug.Log it first, to see what that calculation yields. That's the only way to debug these things - study each component one by one.

avatar image superventure · May 26, 2011 at 07:32 AM 0
Share

Well, I did suspect integers being a problem, but then I went and added '.0' to the end of the power variable. Numbers divisible by 3 work, others give me calculations under 1.0 ... Is there any way to round off the numbers we divide by in the equation so that integers can be used?

avatar image CHPedersen · May 26, 2011 at 07:47 AM 2
Share

Well, first of all, don't do those divisions. :) Division is actually computationally slower than multiplication, by more than 2x, so try to convert stuff into multiplications instead, especially in loops.

Second of all, I'm admittedly not a Js-programmer and I never code anything that's dynamically typed, but couldn't you simply write "var power : float;" to enforce that this is a floating point type? Then, any arithmetic operation you perform on it with a constant (such as multiplication by 0.6666) should also return a float without need of further casting.

avatar image superventure · May 26, 2011 at 08:22 AM 0
Share

The odd thing is and I just now realized, the '/3' works just fine except for in a specific onTrigger function. It refuses to calculate in any form of decency, and it is the exact same code! I also tried using float as well, but nothing changed. I think I do need a multiplication solution for this, I just don't know how to write it out.

Show more comments
avatar image

Answer by TowerOfBricks · May 26, 2011 at 07:57 PM

 var power : float = 100;

 slam : float = Random.Range (0.66666*power,power);
 Debug.Log ("A number in between 2/3 of "+power+" and "+power+" is for example "+slam);

That would work.

You could also do:

 slam : float = power-Random.value*power*0.3333;
Comment
superventure

People who like this

1 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Unity3d: Random.Range() Problem 1 Answer

Is Random.Range really uniform? 1 Answer

Random.seed Repeating 2 Answers

Random Range Seems... Unrandom 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