To show 25 images randomly out of 40 images in a grid view

前端 未结 4 1009
醉梦人生
醉梦人生 2021-01-24 15:45

I am creating an iphone app in which I need to create a grid view of 25 images. I am doing that by taking 25 images in an array, and displaying them by using for loop by changin

4条回答
  •  情深已故
    2021-01-24 16:08

    Create an array of 41 numbers (0-40), shuffle them using a partial Fisher-Yates shuffle and use the first 25 elements of the array.

    Pseudo-code (random(x) returns a random number from 0 to x inclusive):

    array = [0, 1, 2, ..., 40]
    for i in 0, 1, ..., 24 do
        swap array[i], array[i + random(40 - i)]
    truncate array to 25 elements.
    

提交回复
热议问题