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.


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.

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;