- Home /

# Math question about random range and fractions

How do you say 2/3 of a number to give in random range? This is what I tried and it seemed to work at first, but this is a no go.

```
var power = //this is an inspector input number
Slam = Random.Range(power/3 + power/3, power +1);
```

I am basically trying to say that I want slam to be a random number between 2/3s of power and total power.

Thanks

### People who like this

**Answer** by CHPedersen
·
May 26, 2011 at 07:05 AM

I just tested this (in C#), it worked fine for me and seems to do what you want:

```
int test = 5;
float Slam = Random.Range(0.6666f * test, test);
```

The reason it didn't work for you is possible due to your division getting truncated to an integer, if `power`

is dynamically typed to an integer.

Notice that,

```
int test = 5;
Debug.Log(test / 3);
```

Outputs "1", because `(int)(5/3)`

= 1.

What about in Js? I tried .6666 and it's still giving me weird results.

Can you elaborate on "weird results"? Try Debug.Log'ing both the precise input and the results, to see what is actually being produced. If you have 0.6666f*power taking place inside the call to Random.Range, then move it out and Debug.Log it first, to see what that calculation yields. That's the only way to debug these things - study each component one by one.

Well, I did suspect integers being a problem, but then I went and added '.0' to the end of the power variable. Numbers divisible by 3 work, others give me calculations under 1.0 ... Is there any way to round off the numbers we divide by in the equation so that integers can be used?

Well, first of all, don't do those divisions. :) Division is actually computationally slower than multiplication, by more than 2x, so try to convert stuff into multiplications instead, especially in loops.

Second of all, I'm admittedly not a Js-programmer and I never code anything that's dynamically typed, but couldn't you simply write "var power : float;" to enforce that this is a floating point type? Then, any arithmetic operation you perform on it with a constant (such as multiplication by 0.6666) should also return a float without need of further casting.

The odd thing is and I just now realized, the '/3' works just fine except for in a specific onTrigger function. It refuses to calculate in any form of decency, and it is the exact same code! I also tried using float as well, but nothing changed. I think I do need a multiplication solution for this, I just don't know how to write it out.

**Answer** by TowerOfBricks
·
May 26, 2011 at 07:57 PM

```
var power : float = 100;
slam : float = Random.Range (0.66666*power,power);
Debug.Log ("A number in between 2/3 of "+power+" and "+power+" is for example "+slam);
```

That would work.

You could also do:

```
slam : float = power-Random.value*power*0.3333;
```

### Your answer

### Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. **Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.**

For more information, please read our full announcement.

### Follow this Question

### Related Questions

Distribute terrain in zones 3 Answers

Unity3d: Random.Range() Problem 1 Answer

Is Random.Range really uniform? 1 Answer

Random.seed Repeating 2 Answers

Random Range Seems... Unrandom 1 Answer