随机数发生器碰撞

时间:2017-10-11 23:36:36

标签: c#

我实现了一种随机生成自动字母和数字的方法。

user_number = int(input('Please enter a number between 1 & 7: '))
DayDict = {1:'Monday',2:'Tuesday',3:'Wednsday',4:'Thursday',5:'Friday',6:'Saturday',7:'Sunday'}

def num_day(x):
     if x in DayDict:
         print 'The number '+ str(x) + ' corresponds to '  + DayDict[x]
     else:
         print 'Sorry, that is not a valid number.'

num_day(user_number)

此randomNumberGenerator类会产生冲突的概率是多少?

1 个答案:

答案 0 :(得分:0)

这取决于:foresttutorial cls echo. echo __________________________ echo | | echo | An enemy appeared! | echo | Enemy health: %enhealth% | echo | Your health: %yourhealth%| echo | | echo | 1. ATK 2. USE POT | echo | 3. GOLD 4. PASS | echo | _________________________| echo. echo Here appears an attack screen! echo Quick, press 1 and enter! set /p choice8 if %choice8% == 1 goto atkforesttutorial1 else goto invalidforesttutorial1 :invalidforesttutorial1 cls echo Invalid answer. You can try the other options soon, echo for now just press 1. pause goto foresttutorial :atkforesttutorial1 set /a roll=(%random% %% 7) + 1 set enhealth = %enhealth% - %roll% echo You attacked the enemy! echo. echo __________________________ echo | | echo | An enemy appeared! | echo | Enemy health: %enhealth% | echo | Your health: %yourhealth%| echo | | echo | 1. ATK 2. USE POT | echo | 3. GOLD 4. PASS | echo | _________________________| echo. pause 的实现,但是现在,我认为碰撞的概率是100%,因为你在每次调用时实例化一个新实例。如果您使用的是System.Random,那就是try it out on DotNetFiddle

RandomNumberGenerator

输出(哎呀!):

public class Program
{
    public static string RandomNumber()
    {
        var rng = new System.Random();
        var bytes = new byte[8];

        rng.NextBytes(bytes);

        return Convert.ToBase64String(bytes);
    }

    public static void Main()
    {
        Console.WriteLine(RandomNumber());
        Console.WriteLine(RandomNumber());
    }
}

我的建议是仅实例化EgNMBvntr1w= EgNMBvntr1w= 的一个实例,并在程序的整个生命周期中将其保存在静态变量中。如果你这样做,赔率将更像是1比2 ^ 64。

RandomNumberGenerator