Generating Unique Random Numbers in an Array using Loop

后端 未结 6 2026
臣服心动
臣服心动 2020-12-12 01:36

So the question is to develop a [5][5] table, each containing unique numbers from 1-100 (no duplicates)

so here\'s what I came up with:

#inc         


        
6条回答
  •  眼角桃花
    2020-12-12 01:51

    Just create array of boolean of size 100 : bool numberUsed[100]. Then in cycle:

    1.Generate random number r
    2.If numberUsed[r] is true, dont add that r anywhere and continue in loop
    3.numberUsed[r] = true
    

    Note that you need to use WHILE cycle with this approach, not FOR cycle.

提交回复
热议问题