As I understand it, Random.Next() uses the system time for getting the seed, but when iterating through a loop very fast the system time hasn’t changed or has hardly changed, giving me the same “random” number. I’m attempting to use random to select starting positions to begin writing bytes for about 2 seconds of static here and there to music files, 30 different positions are selected, but they’re almost the same. I’m getting almost continuous static from the beginning, broken about 3 times for just a few seconds before it resumes playing the music normally at around 30 seconds in; which isn’t what I want, I need it spread out throughout the entire clip. “int pos” is the problem, its not random, each starting position is nearly identical to all the others, so I have a prolonged amount of static, not static randomly spread throughout the music. My randoms are also static.
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite);
for (int i = 0; i < 20; i++)
{
int pos = rand2.Next(75000, 4000000 /*I've been too lazy to get the file length, so I'm using 4000000*/ );
for (int x = 0; x < 500000/*500000 is a little over 2 seconds of static*/; x++)
{
byte number = array[rand.Next(array.Length)];
stream.Position = pos;
pos++;
stream.WriteByte(number);
}
}
I assumed that it would take 5 seconds or so to make each write (on my slow CPU), which would be enough time for the next random to give me a position that isn’t identical to or extremely close to the previous one. As it stands, each time I seem to be getting an initial position of about ~90000 (first few seconds of music); and all of the next ones are within 20 seconds of that. So my question is, what do I need to modify/do differently in order to achieve my desired result? I’d like a couple seconds of static scattered throughout the entire clip, not clustered together.
I have a byte array which stores my hex digits, which are randomly selected, that appears to work fine, its just the randomness of the writing positions isn’t random at all, they’re all in extreme proximity.
byte[] array = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
Thanks.
P.S. I know I should have using blocks for the FileStream, I’ll add it when I get around to it.
If 500000 bytes is about 2 seconds of audio, and you’re starting somewhere between 75000 and 4000000, you’re starting between 0.15s and 8s into the song. That explains why you’re not hearing any static after about 10s into the song. Try using the actual file size (minus 500000) as the upper bound of the rand.Next call used to populate
pos.