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
0
Question by Darkwinger · Aug 30, 2016 at 05:04 PM · random.rangerangecharacterschancepercent

Using a range of numbers in a variable?

I'm essentially making a Crossy Road type character-creator, and I'm using random range to determine a number, but is it possible to have a range of ints in the chances, to make rarities. Sort of like this:(the colon means to, i.e. 1 to 10)

 if (cash > 100) {
 
         selection = Random.Range (1, 100;
             if (selection = 1:10){
                 Debug.Log("Common Character1");
             }
             if (selection = 11:15){
                 Debug.Log("Rare Character1");
             }
             if (selection = 100){
                 Debug.Log("Legendary3");
             }
             }

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
1
Best Answer

Answer by juicyz · Aug 30, 2016 at 05:40 PM

 float selection = 0.0;
 if (cash > 100) {
    selection = Random.Range (1, 100);
    if (selection <= 10) {
       // If selection is 0 - 10 then...
       Debug.Log("Common Character1");
    }
    else if (selection <= 15) {
       // If selection is 10 (exclusive) - 15 then...
       Debug.Log("Rare Character1");
    }
    else if (selection == 100) {
       // If selection is 100 then...
       Debug.Log("Legendary3");
    }
    else {
       Debug.Log("No Character for you, haha jkjk");
    }
 }


If that doesn't make sense to you... (the above is probably the best way) you could also do the following.... which is the same as I did above

 float selection = 0.0;
 if (cash > 100) {
    selection = Random.Range (1, 100);
    if (selection >= 1 && selection <= 10) {
       // If selection is 0 - 10 then...
       Debug.Log("Common Character1");
    }
    if (selection >= 11 && selection <= 15) {
       // If selection is 11-15 then...
       Debug.Log("Rare Character1");
    }
    if (selection == 100) {
       // If selection is 100 then...
       Debug.Log("Legendary3");
    }
 }

Be aware that selection returns a float, which is a decimal value, so it can return 10.4. The second examples if statements won't apply but that is the example that you wanted. You can change the numbers to be correct and include all the values.

There are a lot of different ways to write this. All up to you.

Comment
Add comment · Show 10 · 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 Cynikal ♦ · Aug 30, 2016 at 06:00 PM 0
Share

Per your example of:

 if (selection <= 10) {
        // If selection is 0 - 10 then...
        Debug.Log("Common Character1");
     }
     if (selection <= 15) {
        // If selection is 11-15 then...
        Debug.Log("Rare Character1");
     }
     if (selection == 100) {
        // If selection is 100 then...
        Debug.Log("Legendary3");
     }

is not a good one.

If Selection is Less than or equal to 10... will work fine.

However, for your selection

If your selection is 3..it'll trigger both. Your second example however is spot on.

And yes,@Darkwinger you should change selection from float to int for nice whole numbers.

avatar image juicyz Cynikal ♦ · Aug 30, 2016 at 06:28 PM 0
Share

Wow, idk what happened... $$anonymous$$y first example had 'else if' in there then when I edited it a couple of times they got removed I guess accidentally. Thanks for catching that.

avatar image Darkwinger · Aug 30, 2016 at 09:52 PM 0
Share

@juicyz I feel like such a doofus now, if I remembered that there was > and < I would have the script done before I could finish writing my question. Thanks so much for pointing me in the right direction. Though I probably need to take a break and get my head straight...

avatar image Darkwinger · Aug 30, 2016 at 10:04 PM 0
Share

@Cynikal I was already using ints, but didn't have the variables shown in my script, but juicyz changed it to float, and @ juicyz Floats give numbers to six digits, and have a decimal number without an f means it's a double and can have 12 digits. So with float you could get 99.9999 and not get the legendary...But I might like those odds. So actually it's unlikely you would get 10.4 just, now I feel like I'm just nitpicking for no reason so I'm just gonna end this, with a comma, comma.

avatar image Darkwinger · Aug 30, 2016 at 10:06 PM 0
Share

$$anonymous$$ind of off track, but what are rep. points?

avatar image juicyz Darkwinger · Aug 30, 2016 at 11:24 PM 0
Share

Just points you gather when you answer questions. Don't think they have a purpose besides once you have +15rep, your post doesn't need to be approved after that.

avatar image Cynikal ♦ Darkwinger · Aug 30, 2016 at 11:31 PM 0
Share

Once you get enough points (1,000 I believe) you become a low level tier moderator.

It also just tracks general help of people within the community.

avatar image Darkwinger · Aug 30, 2016 at 10:18 PM 0
Share

@juicyz Btw, ins$$anonymous$$d of using multiple ifs couldn't I use a switch case? I just never got round to learning how, so I've always used multiple ifs, so if you could show me how, just for my self accomplishment.

avatar image juicyz Darkwinger · Aug 30, 2016 at 11:28 PM 0
Share

In this case I don't think you should unless you are going to have a standard size deviation. If they are going to be random like 0-10, 11-15, 16-30, etc etc then you shouldn't use a switch case.

If you are going to do something like 0-10, 11-20, 21-30, 31-40, etc etc then you could use a switch case. For example:

 int range = (num - 1) / 10; // Since the ranges are in groups of 10 you divide by 10
 switch(range) {
   case 0: // 0-10
     break;
   case 1: // 11-20
     break;
   default:
     break;
 }

I think if statements are cleaner and easier to follow in this case

avatar image Darkwinger juicyz · Aug 31, 2016 at 03:38 PM 0
Share

I'm going to be using rarities anyway, so it looks like I'm not going to use a switch case,(I liked ifs better anyway), though thanks for clearing up how to use switch cases. I feel like I should've known that before, now I feel like a more rounded coder.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Random Range a Float? 2 Answers

newbie question how would get the percentage of its original starting value over its current value 1 Answer

Is Random.Range really uniform? 1 Answer

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

[Beginner] Using Random.Range in initial game object position 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