Answer the question
In order to leave comments, you need to log in
Values are not selected randomly from 2 arrays, how to fix?
There are 2 arrays from which values should be selected randomly. However, the index of a value from one array always matches an index from another array. What needs to be done so that the indexes of the elements from each array are different?
using System;
namespace Example
{
class Program
{
static void Main(string[] args)
{
Start: string[] name = new string[10]
{
"имя1",
"имя2",
"имя3",
"имя4",
"имя5",
"имя6",
"имя7",
"имя8",
"имя9",
"имя10"
};
Random rand_name = new Random();
int index_n = rand_name.Next(name.Length);
string n = name[index_n];
string[] name2 = new string[10]
{
"фамилия1",
"фамилия2",
"фамилия3",
"фамилия4",
"фамилия5",
"фамилия6",
"фамилия7",
"фамилия8",
"фамилия9",
"фамилия10"
};
Random rand_name2 = new Random();
int index_n2 = rand_name2.Next(name2.Length);
string n2 = name2[index_n2];
Console.WriteLine($"{n} {n2}");
Console.ReadKey();
goto Start;
}
}
}
Answer the question
In order to leave comments, you need to log in
Since the numbers are not random, but pseudo-random, a certain seed is used to generate them, which is the same within one calculation cycle. The solution is not to create a second instance of Random, but to use the previous one: int index_n2 = rand_name.Next(name2.Length);
PS Don't use goto, it's bad practice in C#
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question