jeudi 18 octobre 2018

c# Random gives the same number [duplicate]

This question already has an answer here:

My program generates a random integer between 0 and 1000 using System.Random at second once. The problem is, because it generates in the same millisecond time, the value always the same. How can I get a random number if the milliseconds are the same? I tried using some seeds like Math.sqrt(DateTimeOffset.Now.ToUnixTimeMilliseconds()) but there is a way what more fancy and doesn't consuming that much CPU like sqrt method?

Here's the code:

int x = new Random(0).Next((int)(radius + 0.5), (int)(Program.width - radius * 2 - 0.5));
int y = new Random(1).Next((int)(radius + 0.5), (int)(Program.height - radius * 2 - 0.5));




Aucun commentaire:

Enregistrer un commentaire