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
0
Question by $$anonymous$$ · Nov 14, 2014 at 03:46 AM · randomrandom.range

Random between -1 and 1

I am trying to get a random float between -1 (inclusive) and 1 (inclusive).

I have tried Random.Range(-1, 1), but this just provides -0.1 or 0.

I have tried Random.Range(-1.0F, 1.0F), but this just provides between -0.1 and 0.1

Two questions:

Why are these the results? How can I get a random float between -1 and 1?

Details:

I am just calling the Random methods in my update method and printing them out like such:

 void Update(){
     float randomNumber = Random.Range(#, #) * 1;
     Debug.log(randomNumber);
 }


Update:

It is not happening unless I multiply by 1...what???

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 Eric5h5 · Nov 14, 2014 at 05:21 AM 0
Share

I have tried Random.Range(-1.0F, 1.0F), but this just provides between -0.1 and 0.1

That's untrue; it returns -1.0 through 1.0. Also, multiplying anything by 1 is just itself, so don't bother doing that (the compiler probably optimizes that out anyway).

2 Replies

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

Answer by Bunny83 · Nov 14, 2014 at 04:19 AM

Uhm, if i use your code (once fixed all typing errors) i get values between -1 and 1. Of course you have to use the float variant of the Range method, otherwise it will use the integer overload.

 float randomNumber = Random.Range(-1f,1f);
 Debug.Log(randomNumber);

So i can't reproduce your results.

Comment
Add comment · Show 5 · 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 Bunny83 · Nov 14, 2014 at 04:26 AM 1
Share

btw: Random.Range(-1, 1) will only output two values: -1 and 0 as in this case you don't use the float overload but the integer overload which is $$anonymous$$ inclusive and max exclusive and will only return integer values.

avatar image $$anonymous$$ · Nov 14, 2014 at 04:37 AM 0
Share

It is different when you multiply randomNumber by 1, oddly enough

 float one = 1;
 float randomNumber = Random.Range(-1f,1f) * one;
 Debug.Log(randomNumber);


And sorry for having typos above. I was unaware

avatar image Bunny83 · Nov 14, 2014 at 05:23 AM 0
Share

@$$anonymous$$: Uhm, no i can't reproduce this neither. I always get values between -1f and 1f, no matter if i multiply it with 1, 1f or a float variable containing 1f. Are you sure you execute the correct testing code? In general it's not recommended to just Log a single number. It could come from a different test you did (and forgot) and your actual test isn't executed at all. You should always do something like:

 Debug.Log("Random.Range(-1f,1f) = " + randomNumber);

That way you can be sure that the debug log actually comes from this piece of code.

avatar image Eric5h5 · Nov 14, 2014 at 05:46 AM 0
Share

Well, clicking on the debug output in the console will take you to the line of code that printed the log.

avatar image $$anonymous$$ · Nov 14, 2014 at 05:55 AM 0
Share

Well I am not sure what is going on if you are unable to reproduce. I must have some well hidden bug. Either way, I got it working now by changing 1 to 1.0F...soooo thanks :)

avatar image
1

Answer by Dragonsnare0 · Nov 14, 2014 at 05:56 AM

Random.value gives you a random point between 0 - 1. Try

 Random.value*2-1

There you go. random from -1 to 1

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

28 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

Related Questions

Generate random no and active that GO using array 1 Answer

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

How can I Instantiate and do it in a random spot in a random spot? 2 Answers

Teleport to a random height 1 Answer

Random target not working 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