Random Number Generation for Multiple Objects

So, I am trying to create a matrix program with "rainy green code". Everything went well until I decided to drop all instances of the rows into the list and extract / update it. To achieve the proper effect, I need to randomize several things.

All lines are created and saved in the list that you see here in a for loop. Random numbers with intervals and random drops change how quickly the line falls and how fast individual characters rotate through the sprite sheet.

For some reason, although I just get the text that drops right away and all the sprites rotate the same way. Classes and their corresponding functions work ... so the question is what am I doing wrong with my initialization of random numbers?

for (int i = 0; i < (wWidth / 30); i++) { Random random = new Random(new System.DateTime().Millisecond); float randInterval = NextFloat(random); int dropSpeed = random.Next(1, 7); _msList.Add(new MatrixString(chinese, randInterval, dropSpeed, dropSpeed, 1.0f, xOff, 10)); xOff = i * 32; } 
+4
source share
2 answers

You need to create a random instance outside the for loop:

 Random random = new Random(new System.DateTime().Millisecond); for (int i = 0; i < (wWidth / 30); i++) { float randInterval = NextFloat(random); int dropSpeed = random.Next(1, 7); _msList.Add(new MatrixString( chinese, randInterval, dropSpeed, dropSpeed, 1.0f, xOff, 10)); xOff = i * 32; } 

In a short duty cycle, seed using new System.DateTime().Millisecond will produce the same initial value. Therefore, one and the same random number.

+6
source

Your loop is "fast", and therefore new Random(new System.DateTime().Millisecond) will always get the same result as each loop is faster than 1 ms - the following should work:

 Random random = new Random(new System.DateTime().Millisecond); for (int i = 0; i < (wWidth / 30); i++) { float randInterval = NextFloat(random); int dropSpeed = random.Next(1, 7); _msList.Add(new MatrixString(chinese, randInterval, dropSpeed, dropSpeed, 1.0f, xOff, 10)); xOff = i * 32; } 
+2
source

Source: https://habr.com/ru/post/1392203/


All Articles