There is an array with indices from 1 to 9. Each index is a chance multiplied by 10 (that is, index 1 is 10% chance, 2 is 20% and up to 9 = 90%) that this index will be chosen, the task is to determine the index.

I came to this solution: Create an array in which the number of values ​​will be 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] ... 

Next, make the suffle of the array (though in fact it is not necessary).

Next do $ rand = rand (1, count (arr))

And the result will be arr [$ rand] - the value will be the array index from 1 to 9 ...

The question is: did I understand everything correctly, and if so, what alternatives are simpler ...

ADDITION FOR NOT UNDERSTANDING THE TASK:

There are 9 doors in front of you, each door has a number 1, 2, 3, 4 ... 9. The chance that you go in the door with the number 1 = 10%, the chance that you go in the door with the number 2 = 20%, the chance that you go in the door with the number 7 = 70% and so on to the door with the number 9 which has a 90% chance that you will go there. Which door will you enter?

As option number 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% 

This is all! figured out ...

  • Listen to these words: "index 1 is 10% chance, ... 9 = 90%". So 1 and 9 together 100%. And the rest? How many percent do you have? - Igor
  • I think the problem is purely mathematical and to find the "chance for an array index" you need to solve this problem: В мешке находится 1 шар с цифрой 1, 2 шара с цифрой 2, 3 шара с цифрой 3 .... 9 шаров с цифрой 9. Какова вероятность вытащить шар с каждой цифрой? - MasterAlex
  • one
    But what about the condition of the problem (no one reads)? Calculate the array index from 1 to 9, if each index has its own chance from 10% to 90% - bsbak
  • five
    @ user3737786 - As long as you get 450%, you do not solve the problem. - Igor
  • one
    @MasterAlex, something came to mind about Carroll's Wonderland :) - user207618

2 answers 2

Why just if you can be difficult :)

Here, in C / C ++:

 int idx = int((sqrt(20.0*(rand()%450)+25)-5)/10.0 + 1); 

Here is the distribution of 10,000 numbers obtained by this method:

 1 214 2 439 3 673 4 903 5 1084 6 1330 7 1649 8 1782 9 1926 

    And if you don’t need to consider the probability, but you just need to pull a ball with a number out of the bag, then you can come up with such a quick solution to javascript (I don’t pretend to decide a month, I think there are more flexible options):

     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]); 

    • You wrote the code also, in fact, as I suggested. - bsbak
    • one
      From a logical point of view as well, there can be no other logic, but from a technological point of view it is different, you immediately fry the meat, and I first marinate a little, and then fry :) - MasterAlex