This question already has answers here:
filling a array with random numbers between 0-9 in c# [duplicate]
(3 answers)
Closed 5 years ago.
So I have myself an array like this one:
int masterNumber = randomNumber.Next(1, 7);
int childNumber;
int[] fieldArray = new int[] {masterNumber, childNumber = randomNumber.Next(1, 7)};
So now i'd like to write the master number to my console ONLY ONCE using Console.WriteLine(); and I'd like to write the random childNumber 99 times to the console, each childNumber with it's own value between 1 and 7. How would I do this?
Why you want to use fieldArray if you have "masterNumber" & "childNumber". But I think, you can do it in this way...
Random randomNumber = new Random();
int masterNumber = randomNumber.Next(1, 7);
int childNumber;
int[] fieldArray = new int[] { masterNumber, childNumber = randomNumber.Next(1, 7) };
Console.Write("Master Number " + masterNumber);
int i = 0;
while (i < 99)
{
Console.Write("Child Number " + childNumber);
Console.WriteLine(i);
i++;
}
Related
This question already has answers here:
How do I generate a random integer in C#?
(31 answers)
Closed 5 years ago.
I've tried everything from YouTube videos to this forum.
I used to find always a solution here but now I'm stuck.
I need to generate a random number between 39 and 52.
Here's the somewhat source:
case Form1.number.WITHRANDOM:
{
int i = 0;
while (i < ammount)
{
i++;
int j = 0;
string text2 = "";
while (j < 2)
{
string value = Conversions.ToString(this.random.Next(0, text.Length));
text2 += Conversions.ToString(text[Conversions.ToInteger(value)]);
j++;
}
this.numberList.Add("173" + (The random number) + text2);
}
break;
}
You should use the Random class. Its Next method returns a random integer within a specified range (between minValue and maxValue):
public virtual int Next(int minValue, int maxValue)
So, in your case, this is the code:
Random random = new Random();
int number = random.Next(39, 52);
This question already has answers here:
C# get all elements of array
(10 answers)
Closed 5 years ago.
I'm using this code but it writes the array number instead of its value.
int[] vektor = new int[7];
vektor[0] = 5;
vektor[1] = 12;
vektor[2] = 8;
vektor[3] = 18;
vektor[4] = 6;
vektor[5] = 9;
vektor[6] = 22;
Random v = new Random();
int tal = v.Next(vektor.Length);
System.Console.WriteLine(tal);
It looks like you're just outputting the number, not the item from teh array
System.Console.WriteLine(vektor[tal]);
This question already has answers here:
Generating random, unique values C#
(17 answers)
Closed 5 years ago.
I have managed to make an app which randomly generates a selected amount of numbers and puts all of the numbers in a rich text box (A result I would get from 1-100 with 10 numbers is '67, 55, 28, 35, 7, 69, 47, 59, 69, 82'. However, I am wanting to add an option where you can select if you want the numbers to be unique (checkbox), because the numbers box is close to the max number box, the numbers tend to duplicate.
I am not too sure how to do this though, I have tried looking online but most of the answers are a bit too complicated for me.
The code I have so far (C# Windows Form App FYI):
int minComplexNumber = Convert.ToInt32(minComplexTextBox.Text);
int maxComplexNumber = Convert.ToInt32(maxComplexTextBox.Text);
int intergersNumber = Convert.ToInt32(intergersTextBox.Text);
int numbersLeft = intergersNumber;
resultComplexTextBox.Text = "";
if (UniqueCheckBox.Checked)
{
//Need something here
}
else
{
Random comrnd = new Random();
while (numbersLeft > 1)
{
int complexResult = comrnd.Next(minComplexNumber, maxComplexNumber);
resultComplexTextBox.Text += complexResult + ", ";
numbersLeft = numbersLeft - 1;
}
if (numbersLeft == 1)
{
int complexResult = comrnd.Next(minComplexNumber, maxComplexNumber);
resultComplexTextBox.Text += complexResult;
numbersLeft = numbersLeft - 1;
}
}
Any advice on how to get unique numbers?
I like the #David suggestion, you can go with something like this:
var random = new Random();
var possibilities = Enumerable.Range(1, 100).ToList();
var result = possibilities.OrderBy(number => random.Next()).Take(10).ToArray();
Console.WriteLine(String.Join(",", result));
Use a List<int> to store the list of available numbers, then take and remove as many as you need from there:
//if checked...
Random comrndu = new Random();
var available = Enumerable.Range(minComplexNumber, (maxComplexNumber - minComplexNumber) + 1).ToList();
while (numbersLeft-- > 0)
{
int idx = comrndu.Next(0, available.Count);
int complexResult = available[idx];
available.RemoveAt(idx);
resultComplexTextBox.Text += complexResult + ", ";
}
resultComplexTextBox.Text = resultComplexTextBox.Text.Substring(0, resultComplexTextBox.Text.Length - 2);
quick and dirty solution: generate number and put it into list (if it doesn't already exist).
When done, write all numbers to texbox.
Solution that David suggested in comment is better. This is, i repeat, quick and dirty and could help you if there's not many numbers to generate
if (UniqueCheckBox.Checked)
{
Random comrnd = new Random();
List<int> generatedNumbers = new List<int>();
while (numbersLeft > 0)
{
int complexResult = comrnd.Next(minComplexNumber, maxComplexNumber);
if (!generatedNumbers.Contains(complexResult)){
generatedNumbers.Add(complexResult);
numbersLeft = numbersLeft - 1;
}
}
resultComplexTextBox.Text += string.Join(", ", generatedNumbers.ToArray());
}
This question already has answers here:
Random.Next returns always the same values [duplicate]
(4 answers)
Closed 9 years ago.
I am trying to create a range of non duplicate random numbers between 1 - 10, I planned on doing this by storing each random number I made in to an array and then checking that array every time to make sure I ain't already used the number.
My problem is that instead of creating different random numbers such as 1, 2, 3 I just keep getting the same random number over and over.
randomNumber();
Label1.Text = randomRow + "";
randomNumber();
Label2.Text = randomRow + "";
randomNumber();
Label3.Text = randomRow + "";
public int randomNumber()
{
List<int> numbers = new List<int>();
int num = 0;
Random randNum = new Random();
num = randNum.Next(1, 11);
if (numbers.Contains(num))
{
num = randNum.Next(1, 11);
}
else
{
randomRow = num;
numbers.Add(num);
}
return randomRow;
}
Problem : everytime you are creating the RandomNumber object in too close time.
When you create a Random object, it's seeded with a value from the system clock. If you create Random instances too close in time, they will all be seeded with the same random sequence.
From Here
When you create a Random object, it's seeded with a value from the
system clock. If you create Random instances too close in time, they
will all be seeded with the same random sequence.
Solution :
move Random randNum = new Random(); outside the function randomNumber().
Try This:
Random randNum = new Random();
public int randomNumber()
{
List<int> numbers = new List<int>();
int num = 0;
num = randNum.Next(1, 11);
if (numbers.Contains(num))
{
num = randNum.Next(1, 11);
}
else
{
randomRow = num;
numbers.Add(num);
}
return randomRow;
}
My best gues is that you are using this in a loop. In this case because you declare
Random randNum = new Random();
evry time this will generate tha same number. Just declare it BEFORE the loop and it should be fine.
Also you should consider a different approch because it is not a good practice. Like:
int[] array = {1,2,3,4,5,6,7,8,9,10};
Random randNum = new Random();
int rand=0;
int temp;
for(int i = 0; i<10;i++)
{
rand = randNum.next(1,10-i);
temp=array[rand];
array[rand]=array[9-i];
array[9-i]=temp;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
filling a array with uniqe random numbers between 0-9 in c#
I have a array like "page[100]" and i want to fill it with random numbers between 0-9 in c#...
how i can do this?
i used :
IEnumerable<int> UniqueRandom(int minInclusive, int maxInclusive)
{
List<int> candidates = new List<int>();
for (int i = minInclusive; i <= maxInclusive; i++)
{
candidates.Add(i);
}
Random rnd = new Random();
while (candidates.Count > 1)
{
int index = rnd.Next(candidates.Count);
yield return candidates[index];
candidates.RemoveAt(index);
}
}
this way :
int[] page = UniqueRandom(0,9).Take(array size).ToArray();
but it just gave me 9 unique random numbers but i need more.
how i can have a array with random numbers that are not all the same?
How about
int[] page = new int[100];
Random rnd = new Random();
for (int i = 0; i < page.Length; ++i)
page[i] = rnd.Next(10);
Random r = new Random(); //add some seed
int[] randNums = new int[100]; //100 is just an example
for (int i = 0; i < randNums.Length; i++)
randNums[i] = r.Next(10);
You have an array of 100 numbers and draw from a pool of 10 different ones. How would you expect there to be no duplicates?
Don't overcomplicate the thing, just write what needs to be written. I.e.:
Create the array
Loop over the size of it
Put a random number between from [0, 9] in the array.