Picking 2 random elements from array

前端 未结 9 932
一向
一向 2020-12-16 18:32

What is the most efficient way to select 2 unique random elements from an array (ie, make sure the same element is not selected twice).

I have so far:



        
9条回答
  •  無奈伤痛
    2020-12-16 19:11

    do NOT use loops and comparisons. Instead

    • shuffle the array
    • take first two elements

提交回复
热议问题