V
V
voltd gand2019-04-07 10:28:56
PHP
voltd gand, 2019-04-07 10:28:56

How to add rand odds?

For example: rand(1,2);
How to make it so that the probability of dropping 1 was 40%, and 2 60%

Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
DanKud, 2019-04-07
@vladimir_volohov

If specifically for this example, then you can simply do this:

if (rand(1,100) <= 40) {
    echo 1;
} else {
    echo 2;
}

H
holfza, 2019-04-07
@holfza

$random = rand(1,100);
$random <= 40 //1
$random > 40 //2

T
tuwkan, 2019-04-07
@tuwkan

Alternatively rand(1,10)
1-4 = 1
5-10 = 2

A
Andrey Burov, 2017-02-01
@SimpleAutomation

You get Nan because 2 + undefinedthis NaN

function sumTo(n) {
      if(n > 1) {
        return (n + sumTo(n-1));
      }
      return 1;
    }

A
Alexander Manakov, 2017-02-01
@gogolor

Because sumTo(1) without else will return undefined
And undefined + 14 = NaN

A
Alexander Ka, 2017-02-01
@SimpleAutomation

Thanks to !
Am I understanding the chain correctly?
5 + 10 = 15
4 + 6 = 10
3 + 3 = 6
2 + 1 = 3
return 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question