I'm trying to use this method (I found it here Random number generator only generating one random number) to generate unique random number, so I'm not really understand how it works, when I want get value from RandomNumber I got [system.random]
so how to convert it to integer. And also I'm not sure how to use it for several outputs and get unique starting number for each?
private static readonly Random random = new Random();
private static readonly object syncLock = new object();
public static int RandomNumber(int min, int max)
{
lock (syncLock)
{
return random.Next(min, max);
}
}
also I found this solution, but this way I got on output [system.Int32[]]
:
var result1 = Enumerable.Range(1, 101).OrderBy(g => Guid.NewGuid()).Take(10).ToArray();
on output I want get several outputs with different and unique value
out1: 2, 4, 89, 20, ...
out2: 65, 22, 12, 7, ...
to use this values in this code as starting numbers, now I've used just var rand1 = new Random()
which generates similar sequences:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace _02_R
{
class Program
{
static void Main(string[] args)
{
var rand1 = new Random();
var rand2 = new Random();
var start1 = rand1.Next(0, 100);
var start2 = rand2.Next(0, 100);
var incrementor1 = start1 > 50 ? -1 : 1;
var incrementor2 = start2 > 50 ? -1 : 1;
var currentValue1 = start1;
var currentValue2 = start2;
for (var i = 0; i < 1000; i++)
{
if (currentValue1 == 101) incrementor1 = -1;
if (currentValue1 == 0) incrementor1 = 1;
if (currentValue2 == 101) incrementor2 = -1;
if (currentValue2 == 0) incrementor2 = 1;
Console.WriteLine("OUT 1: [" + currentValue1 + "] OUT 2: [" + currentValue2 + "]");
currentValue1 += incrementor1;
currentValue2 += incrementor2;
Thread.Sleep(100);
}
Console.ReadLine();
}
}
}
Aucun commentaire:
Enregistrer un commentaire