Shuffle array in C

前端 未结 7 1805
执念已碎
执念已碎 2020-11-22 03:15

I\'m looking for a function in ANSI C that would randomize an array just like PHP\'s shuffle() does. Is there such a function or do I have to write it on my own

7条回答
  •  别跟我提以往
    2020-11-22 03:48

    There isn't a function in the C standard to randomize an array.

    • Look at Knuth - he has algorithms for the job.
    • Or look at Bentley - Programming Pearls or More Programming Pearls.
    • Or look in almost any algorithms book.

    Ensuring a fair shuffle (where every permutation of the original order is equally likely) is simple, but not trivial.

提交回复
热议问题