Expose a chance for mass index



  • There's a set of indexes 1 to 9. Each index is multiplied by 10 (i.e. index 1 is 10 per cent of the chance, 2 is 20 per cent and 9 = 90 per cent), which selected the index, the task of determining the index.

    I came to this decision: Create a mass in which the number of values is equal to the index number with the index number. Example:

    arr[1] = [1]
    arr[2] = [2]
    arr[3] = [2]
    arr[4] = [3]
    arr[5] = [3]
    arr[6] = [3]
    arr[7] = [4]
    arr[8] = [4]
    arr[9] = [4]
    arr[10] = [4]
    ...
    arr[..] = [7] //не считал какой именно получается номер индекса
    arr[..] = [7]
    arr[..] = [7]
    arr[..] = [7]
    arr[..] = [7]
    arr[..] = [7]
    ...
    

    Then make a suffle of the mass (not necessarily).

    Then make $rand = rand(1,count(arr)

    And the result will be arr[$rand] - the value and the volume index between 1 and 9. ♪

    Question: Is it right if I do, what alternatives are easier? ♪ ♪

    SUPPLEMENTING FOR NON-GOVERNING OBSERVATIONS:

    In front of you, nine doors, each door has a number 1, 2, 3, 4...9. The chance you're gonna walk in the door with number 1 = 10%, the chance you're gonna get in the door with number 2 = 20 percent, the chance you're gonna get in the door with number 7 = 70 percent and so on to the door with room 9, which has a 90% chance that you're gonna get in there. Which door will you come in?

    As option No. 2

    // если не из 45, а из 10
    $randNum = rand(1, 90)
    if($randNum = 1 or $randNum = 2) return 1  // 2%
    elseif($randNum >= 3 and $randNum <= 6) return 2 // 4%
    elseif($randNum >= 7 and $randNum <= 12) return 3 // 6%
    elseif($randNum >= 13 and $randNum <= 20) return 4 // 8%
    elseif($randNum >= 21 and $randNum <= 30) return 5 // 10%
    elseif($randNum >= 31 and $randNum <= 42) return 6 // 12%
    elseif($randNum >= 43 and $randNum <= 56) return 7 // 14%
    elseif($randNum >= 57 and $randNum <= 72) return 8 // 16%
    elseif($randNum >= 73 and $randNum <= 90) return 9 // 18%
    //90%
    

    I've got it. ♪ ♪



  • And if you don't have to count it, you just need to get the room ball out of the bag, javascript You can think of such a solution for an ambulance.

    var arr = [];
    var k = 0;
    var b = 10;
    

    // заполняем мешок шариками
    for (var i = 1; i < b; i++) {
    for (var j = 0; j < i; j++) {
    arr[k] = i;
    k++;
    }
    }

    // Перемешиваем
    function sRand() {
    return Math.random() > 0.5 ? 1 : -1;
    }
    arr.sort(sRand);

    // Вытаскиваем первый шарик
    console.log(arr[0]);




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2