Esempio n. 1
0
        public void CreateFile(string Filename, int FileSizeMb)
        {
            Console.Write("Creating file... ");
            int targetFileSizeBytes = FileSizeMb * 1024 * 1024;

            using (FileStream fileStream = File.Open(Filename, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                using (StreamWriter streamWriter = new StreamWriter(fileStream))
                {
                    // Open root element.
                    int fileSizeBytes = 0;
                    OpenElement(streamWriter, ref fileSizeBytes, false);
                    while (fileSizeBytes < targetFileSizeBytes)
                    {
                        // Determine action.
                        int actionPercent = _random.Next(100);
                        if ((actionPercent < _actionPercentages[0]) && (_elementNames.Count > 1))
                        {
                            CloseElement(streamWriter, ref fileSizeBytes);
                        }
                        else if (actionPercent < _actionPercentages[1])
                        {
                            OpenElement(streamWriter, ref fileSizeBytes);
                        }
                        else
                        {
                            WriteElement(streamWriter, ref fileSizeBytes);
                        }
                    }
                    // Close all elements.
                    CloseAllElements(streamWriter, ref fileSizeBytes);
                }
            }
        }
Esempio n. 2
0
        public static void Shuffle <T>(this IList <T> Items, IThreadsafeRandom Random)
        {
            // Use the Fischer-Yates algorithm.
            // See https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
            var maxIndex = Items.Count - 1;

            for (var index = maxIndex; index > 0; index--)
            {
                var swapIndex = Random.Next(0, index + 1);
                var temp      = Items[index];
                Items[index]     = Items[swapIndex];
                Items[swapIndex] = temp;
            }
        }