Генератор случайных чисел генерирует одинаковые числа - C#

Узнай цену своей работы

Формулировка задачи:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
 
class Rndthread
{
    private int RowsA; private int RowsB;
    private int ColumsA; private int ColumsB;
    private double[,] MatrixA; private double[,] MatrixB;
    private int howMuchFlows;
    private int NumberOfthread;
    static Random WhataNumber = new Random();
    Thread RNDThread;
    public Rndthread(int name/*имя потока*/, double[,] InputmatrixA/*имя матрицы*/, double[,] InputmatrixB, int m, int n, int k, int amountflow)
    {
        RowsA = m; RowsB = n;
        ColumsA = n; ColumsB = k;
        MatrixA = InputmatrixA;
        MatrixB = InputmatrixB;
        howMuchFlows = amountflow;
        NumberOfthread = name;
        RNDThread = new Thread(rndfunction);
        RNDThread.Priority = ThreadPriority.Highest;
        RNDThread.Start();
 
    }
    void rndfunction()
    {
        double[,] mIndex = MatrixA;
        int StartColculation = RowsA / howMuchFlows * (NumberOfthread);
        int EndColculation = RowsA / howMuchFlows * (NumberOfthread + 1) - 1;
        Console.ForegroundColor = ConsoleColor.DarkCyan;
        Console.WriteLine("Матрица А - Поток №{0} ведет расчет от {1} строки до {2} строки и имеет в расположении {3} столбцов....", NumberOfthread + 1, StartColculation, EndColculation, ColumsA);
        Thread.Sleep(0);
        for (int i = StartColculation; i <= EndColculation; i++)
        {
            for (int j = 0; j < ColumsA; j++)
            {
                mIndex[i, j] = WhataNumber.Next(-10, 11);
                Thread.Sleep(0);
            }
        }
        mIndex = MatrixB;
        StartColculation = RowsB / howMuchFlows * (NumberOfthread);
        EndColculation = RowsB / howMuchFlows * (NumberOfthread + 1) - 1;
        Console.ForegroundColor = ConsoleColor.DarkGray;
        Console.WriteLine("Матрица В - Поток №{0} ведет расчет от {1} строки до {2} строки и имеет в расположении {3} столбцов....", NumberOfthread + 1, StartColculation, EndColculation, ColumsB);
        Thread.Sleep(0);
        for (int i = StartColculation; i <= EndColculation; i++)
        {
            for (int j = 0; j < ColumsB; j++)
            {
                mIndex[i, j] = WhataNumber.Next(-10, 11);
                Thread.Sleep(0);
            }
        }
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.WriteLine("Поток №{0} завершил заполнение матрицы случайными числами", NumberOfthread + 1);
    }
}
При данном коде возникает вот такая картина:

Решение задачи: «Генератор случайных чисел генерирует одинаковые числа»

textual
Листинг программы
using System;
using System.Security.Cryptography;
 
// Cryptographically-strong random number generator
// Source: MSDN Magazine > 2007 > September > .NET Matters: Tales from the CryptoRandom
// Source URL: [url]http://msdn.microsoft.com/en-us/magazine/cc163367.aspx[/url]
// via [url]https://gist.github.com/prettycode/5471944[/url]
// Authors: Stephen Toub & Shawn Farkas
public sealed class CryptoRandom : Random, IDisposable
{
    private RNGCryptoServiceProvider cryptoProvider = new RNGCryptoServiceProvider();
    private byte[] uint32Buffer = new byte[sizeof(uint)];
 
    /// <summary>
    /// An implementation of System.Random used for cryptographically-strong random number generation.
    /// </summary>
    public CryptoRandom() { }
 
    /// <summary>
    /// An implementation of System.Random used for cryptographically-strong random number generation.
    /// </summary>
    public CryptoRandom(int seedIgnored) { }
 
    /// <summary>
    /// Returns a nonnegative random number.
    /// </summary>
    /// <returns>
    /// A 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue"/>.
    /// </returns>
    public override int Next()
    {
        cryptoProvider.GetBytes(uint32Buffer);
        return BitConverter.ToInt32(uint32Buffer, 0) & 0x7FFFFFFF;
    }
 
    /// <summary>
    /// Returns a nonnegative random number less than the specified maximum.
    /// </summary>
    /// <returns>
    /// A 32-bit signed integer greater than or equal to zero, and less than <paramref name="maxValue"/>; that is, the range of return values ordinarily includes zero but not <paramref name="maxValue"/>. However, if <paramref name="maxValue"/> equals zero, <paramref name="maxValue"/> is returned.
    /// </returns>
    /// <param name="maxValue">The exclusive upper bound of the random number to be generated. <paramref name="maxValue"/> must be greater than or equal to zero.</param>
    /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="maxValue"/> is less than zero.</exception>
    public override int Next(int maxValue)
    {
        if (maxValue < 0) throw new ArgumentOutOfRangeException("maxValue");
        return Next(0, maxValue);
    }
 
    /// <summary>
    /// Returns a random number within a specified range.
    /// </summary>
    /// <returns>
    /// A 32-bit signed integer greater than or equal to <paramref name="minValue"/> and less than <paramref name="maxValue"/>; that is, the range of return values includes <paramref name="minValue"/> but not <paramref name="maxValue"/>. If <paramref name="minValue"/> equals <paramref name="maxValue"/>, <paramref name="minValue"/> is returned.
    /// </returns>
    /// <param name="minValue">The inclusive lower bound of the random number returned.</param>
    /// <param name="maxValue">The exclusive upper bound of the random number returned. <paramref name="maxValue"/> must be greater than or equal to <paramref name="minValue"/>.</param>
    /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="minValue"/> is greater than <paramref name="maxValue"/>.</exception>
    public override int Next(int minValue, int maxValue)
    {
        if (minValue > maxValue) throw new ArgumentOutOfRangeException("minValue");
        if (minValue == maxValue) return minValue;
 
        long diff = maxValue - minValue;
        long max = (1 + (long)uint.MaxValue);
        long remainder = max % diff;
 
        while (true)
        {
            cryptoProvider.GetBytes(uint32Buffer);
            uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
            if (rand < max - remainder)
            {
                return (int)(minValue + (rand % diff));
            }
        }
    }
 
    /// <summary>
    /// Returns a random number between 0.0 and 1.0.
    /// </summary>
    /// <returns>
    /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
    /// </returns>
    public override double NextDouble()
    {
        cryptoProvider.GetBytes(uint32Buffer);
        uint rand = BitConverter.ToUInt32(uint32Buffer, 0);
        return rand / (1.0 + uint.MaxValue);
    }
 
    /// <summary>
    /// Fills the elements of a specified array of bytes with random numbers.
    /// </summary>
    /// <param name="buffer">An array of bytes to contain random numbers.</param>
    /// <exception cref="T:System.ArgumentNullException"><paramref name="buffer"/> is null.
    public override void NextBytes(byte[] buffer)
    {
        if (buffer == null) throw new ArgumentNullException("buffer");
        cryptoProvider.GetBytes(buffer);
    }
 
    public void Dispose()
    {
        InternalDispose();
    }
 
    ~CryptoRandom()
    {
        InternalDispose();
    }
 
    void InternalDispose()
    {
        if (cryptoProvider != null)
        {
            cryptoProvider.Dispose();
            cryptoProvider = null;
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.375 из 5
Похожие ответы