- Home /
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");
}
}
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.
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.
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.
@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...
@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.
$$anonymous$$ind of off track, but what are rep. points?
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.
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.
@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.
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
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
Follow this Question
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