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
3
Question by guitarxe · Dec 16, 2013 at 01:42 AM · c#random

Getting -1 or 1 randomly without including the 0

I want to get the value of -1 or 1 randomly.

I know we can use Random.Range(min, max) for this, but if I use -1 and 1, then 0 is of course also included, which I do not want.

What simple method is there to get this right? So far I have done two different ways to overcome this problem. I used 1 and 2 for min and max, and if it's 1 then I set the value to -1, if it's something else, then I set the value to +1. The other method is to put the random into a loop, and only quit the loop if the value is not 0.

But I don't think either of these two solutions are probably a good habit to get into, especially the loop one.

Would appreciate any suggestions. Perhaps there's an altogether different method?

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

4 Replies

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

Answer by Bunny83 · Dec 16, 2013 at 01:57 AM

Just do this:

 int someValue = Random.Range(0,2)*2-1
 
 // Random.Range(0,2)       ==  0 or 1
 // Random.Range(0,2)*2     ==  0 or 2
 // Random.Range(0,2)*2-1   == -1 or 1
Comment
Add comment · Show 3 · 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 ed321ali123 · Aug 04, 2021 at 05:12 PM 0
Share

how does it work? please tell me. I want to know!

avatar image Bunny83 ed321ali123 · Aug 04, 2021 at 07:31 PM 0
Share

Uhm I have broken down the code in the 3 comments below the code? What else should I explain?

avatar image Wamoga · Oct 23, 2021 at 02:32 PM 0
Share

in case of random integer the second argument is exclusive. that mean a Random.range(0,50) is a random between 0 and 49. So Random.Range(0,2) can't give you a result of 2, but only 1 or 0.

Then, it's just basic mathematics; (1 2) - 1 = 2 - 1 = 1 (0 2) - 1 = 0 -1 = -1

why is the second argument exclusiv for int and inclusiv for float ? i don't know, probably special cases of mathematics which cause errors.

avatar image
3

Answer by KellyThomas · Dec 16, 2013 at 02:01 AM

You first suggestion is the best option, just put it in a function and forget about the implementation:

 function RandomSign() {
     if (Random.Range(0, 2) == 0) {
         return -1;
     }
     return 1;
 }
Comment
Add comment · Show 2 · 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 Eric5h5 · Dec 16, 2013 at 03:13 AM 1
Share

That won't work; Random.Range(0, 1) will always return 0.

avatar image KellyThomas · Dec 16, 2013 at 03:20 AM 1
Share

@Eric5h5, fair point! Answer updated.

avatar image
1

Answer by enigma_lvn · Feb 23, 2018 at 01:32 PM

Or you could always go:

 public static int RandomSign()
 {
     return UnityEngine.Random.value < 0.5f ? 1 : -1;
 }

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 mayankela21 · Jul 07, 2020 at 10:36 AM 0
Share

thankyou: I modified Your Code And, Use this : public static float RandomSign(float postValue, float negatValue) { return Random.value < 0.5f ? postValue : negatValue; }//it,s HelpFull

avatar image
0

Answer by xandermacleod · Dec 16, 2013 at 02:01 AM

float randomFloat = Random.Range(min, max);

while(randomFloat == 0){ randomFloat = Random.Range(min, max); }

float absFloat = Mathf.Abs(randomFloat); float finalNumber = randomFloat / absFloat;

// finalNumber will be 1 or -1.

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 KellyThomas · Dec 16, 2013 at 02:27 AM 0
Share

$$anonymous$$athf.Sign() would simplify this, but it may still use a division internally.

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

Trouble with Null Reference in Script 2 Answers

Modify mesh problems 1 Answer

move gameobject into a random position and the spawn an enemy 1 Answer

Make AI walk around randomly until Player is Seen. (C#) 2 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