Generating multiple random numbers

后端 未结 5 1614
时光说笑
时光说笑 2021-02-15 11:09

I want to generate 25 unique random numbers and list them in a console. The numbers should be atleast 10 characters long. Any easy way to do that?

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-15 11:49

    There is a big different between Randomness and Uniqueness.

    So if you need really unique numbers you have to make sure that you save somewhere all already created numbers and check if your newly created one isn't within this list or you have to provide some algorithm that ensures that a given number can't created twice.

    To get the second part to work you mostly take the date/time of the creation moment, cause the current date/time pair is unique forever. The only problem is how many creations per (milli)second do you have and how many digits are available to store your unique number.

    A sample about using 12 digits is made here. Hope this helps.

提交回复
热议问题