Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 LeftyTwoGuns · Aug 24, 2014 at 02:42 AM · randombooleanrandom.range

Randomly setting a boolean to true?

Say I'd like to randomly set a boolean to true if an object is hit by a raycast. But I'd like to be able to adjust the chance. I was looking over Random.value. Which, returns a random number between 0.0 to 1.0. Could that represent a random value between 0% and 100% chance? How do I go about this?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Eric5h5 · Aug 24, 2014 at 02:46 AM

 var myBool = (Random.value < 0.5);
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 LeftyTwoGuns · Aug 24, 2014 at 02:51 AM -1
Share

So, that would be something like

 if( myBool < 0.75f){
 
 boolean = true;
 
 }


...?

avatar image Eric5h5 · Aug 13, 2015 at 02:55 PM 0
Share

No:

 var myBool = (Random.value < 0.75f);
avatar image
1

Answer by SinisterRainbow · Aug 13, 2015 at 07:46 AM

C#:

 public void passFail(float fChanceOfSuccess) {
 
    float fRand = Random.Range(0.0f,1.0f);
    if (fRand <= fChanceOfSuccess) 
      return true; 
    return false;
 }



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 Eric5h5 · Aug 13, 2015 at 02:54 PM 1
Share

A lot of excess and redundant code there. It's such a trivial thing you don't need a function; see my answer. But if you had to make it a function for some reason, then it would just be:

 public void PassFail (float chanceOfSuccess) {
     return Random.value < chanceOfSuccess;
 }
avatar image SinisterRainbow · Aug 16, 2015 at 05:38 AM 2
Share

Eric5h5, while I'm sure you are amazing, when I give answers on posts where a person obviously doesn't have a lot of coding experience, I will suffer elegance for clarity.

avatar image Zarkow SinisterRainbow · Nov 10, 2018 at 11:57 AM 1
Share

That attitude is only valid if you also include the 'and this is the better short-form' in the reply, to give the beginner a chance to learn a better coding-style.

avatar image
-3

Answer by SatansPineapple · Sep 26, 2021 at 11:13 PM

my solution with int:

   private void Start()
     {
          for (int i = 0; i < 50; i++)
              {
                  int t = UnityEngine.Random.Range(1, 101);
                  print(t);
                  print(RandomBool(ref t));
                  tiles[i].SetActive(RandomBool(ref t));
              }
     }
     
      //dont do that please dont
      private bool RandomBool(ref int value)
       {
              return value % 2 == 0;
       }
      
      //you can also
      
      private bool RandomBool()
      {
           int value = UnityEngine.Random.Range(1, 101);
           return value % 2 == 0;
       }
 
 

your choice;)

Comment
Add comment · 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 Bunny83 · Sep 27, 2021 at 08:32 AM 0
Share

How is that

Randomly setting a boolean to true

just to quote the question title?

There is no "random" in your code at all. Your method name is misleading. All your method does is checking if the value is even or odd. Also why a ref argument? This makes no sense. The value is not changed inside the method and using a ref here just means it will most likely be slower and also limits the usage of the method since you always need an actual variable to pass to your method.


Though the biggest issue is: This question is 6+ years old, the most recent comment is 3 years old...

avatar image SatansPineapple · Sep 27, 2021 at 01:09 PM 0
Share

hi edited a little bit. who cares about the age of question. im just play around and for me it works well and fast enough. so dont be hate

avatar image Eric5h5 SatansPineapple · Sep 27, 2021 at 04:49 PM 0
Share

It's bad code though, and your edit isn't much better. Playing around is great, but keep it to yourself while learning, since it doesn't really help anyone.

avatar image Zarkow SatansPineapple · Sep 27, 2021 at 05:13 PM 1
Share

You could start by explaining why you pass an int as a ref.

If you don't know why you are doing it, then don't do it.

avatar image SatansPineapple · Sep 27, 2021 at 05:42 PM 0
Share

ok wow thanks for your attention. to the ref keyword. maybe i have misunderstood something. i thought when im writing: void Test(ref int value) it doesnt create an int every time i call this method. so yes my fault.

earlier this day here was a comment: it doesnt work. so i edited post and dont look at redundant code or bad code. yes my fault shame on me:)

so let me try a last time:

 public bool PassFail(int chanceOfSuccess, int condition)
     {
         return UnityEngine.Random.Range(1, 101) % chanceOfSuccess == condition;
     }



avatar image Zarkow SatansPineapple · Sep 27, 2021 at 06:36 PM 0
Share

Why use the Random.range (int) in 1-101 (maxExclusive) and not the native Random in float (0.0-1.0)?

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

27 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

Related Questions

Random spawn script not working correctly 3 Answers

[Beginner] Using Random.Range in initial game object position 1 Answer

How can I do random choices 1 Answer

How to know what random number is chosen 2 Answers

An array of bools with random true and false elements 2 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