3 random numbers into array [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am writing the beginning of my program which is to have the computer generate 3 random numbers on the console.
They need to be 1-9 (including both), no numbers can repeat, and I need to put this answer into an array. What's needed in the main method class vs the class.

You should try it out yourself first (site rules say so), but some hints may be provided:
1) integer random numbers can be generated using Random class. More details can be found here and an answer has been already provided about generation
2) to avoid duplicates each number should be tested against the existing list of numbers:
array.Contains(generatedNumber)
3) For your particular request, an elegant option is to generate all numbers between 1 and 9, shuffle the array and pick the first three elements:
var initArray = Enumerable.Range(1, 9).ToArray();
var randomArray = initArray.OrderBy(x => rnd.Next()).ToArray();
Get first three elements and those are random and distinct.
Generally, you can a subarray using the method specified here.

Try this:
Random rnd = new Random();
int[] arr = Enumerable.Range(0, 10).OrderBy(n => rnd.Next()).Take(3).ToArray();
foreach (var n in arr)
{
Console.WriteLine(n);
}

Try this code below :
//range set 0 to 9
int Min = 0;
int Max = 10;
//declare an array which store 3 random number
int[] arr = new int[3];
Random randNum = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = randNum.Next(Min, Max);
Console.WriteLine(arr[i]);
}

Related

Integer array Get difference between two highest numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a List<int[]> that I populated by splitting an integer array into 4 groups. Now I need to get the difference of the two highest numbers in the array. I tried Array.Sort but I am stuck on how to continue.
What I have done so far?
public static void solution(int[] T)
{
List<int[]> splitted = new List<int[]>();//This list will contain all the splitted arrays.
int lengthToSplit = T.Length / 4;
int arrayLength = T.Length;
for (int i = 0; i < arrayLength; i = i + lengthToSplit)
{
int[] val = new int[lengthToSplit];
if (arrayLength < i + lengthToSplit)
{
lengthToSplit = arrayLength - i;
}
Array.Copy(T, i, val, 0, lengthToSplit);
splitted.Add(val);
}
//this is the part where I must get the difference between the two highest numbers in an integer array and put into another list.
foreach (int[] integerarray in splitted)
{
//get the difference of the two highest numbers per integer array and place it on another List<int>
}
}
get the difference between the two highest numbers in an integer array
and put into another list
You can use LINQ and Math.Abs:
List<int> differenceList = splitted
.Select(list => list.OrderByDescending(i => i).Take(2).ToArray())
.Select(highestTwo => Math.Abs(highestTwo[0] - highestTwo[1]))
.ToList();

generate random GUID ....i want save my file with new guid.tostring [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
how to generate 6 number as => 2 numbers is Const + and 4 numbers is random for Example => 228796 or 225564
First what you intend to be a GUID isn´t a GUID, which is a reversed thing for something like this: "353e1ff6-0493-48f6-953e-15ec5e383034". As of MSDN:
A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required.
Apart from this you can easily create a randomizer that creates numbers between zero and 9999 and use those numbers as your second part:
string constPart = "22";
Random r = new Random();
string myNumber = constPart + r.Next(0, 10000);
You can - even simpler - also use a randomizer for ranges between 220000 and 229999 as follows:
Random r = new Random();
string myNumber = r.Next(220000, 230000).ToString();
Be aware that those numbers aren´t neccessarily unique. That means the more numbers you create, the more does the probability of duplicates increase.
You can simply just use the Random Class.
Make a new Instance of it and use the Next method which has parameters for minimum, maximum.
class Program
{
static void Main(string[] args)
{
int const1 = 1;
int const2 = 2;
Random rng = new Random();
string id = $"{const1}{const2}";
for(int i = 0; i <= 4; i++)
{
id += $"{rng.Next(0, 10)}";
}
Console.WriteLine(id);
Console.ReadKey(true);
}
}

How do i deterministically generate n unique numbers within a range from a GUID? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
A concrete example.
If i have a range of 1-300, how can a generate 5 unique numbers within that range using GUID "EDAAE218-FBF0-4B66-AEAF-FB036FBF69F4". Applying the same algorithm to the GUID should result in the same 5 numbers being chosen every time.
The input doesn't have to be a GUID, it's just acting as some sort of key.
Some context for the problem i am trying to solve. I have a hard coded List of values that contains roughly 300 or so elements. I am trying to find a way to select 20 elements from this list that always produces the same elements.
My idea was to generate a GUID which could be handed out to multiple users. When those users input the GUID into the app, the same 20 elements would be returned for everyone.
A guid is effectively a 128-bit number. So you can easily do this provided that the number of bits required to represent your numbers are fewer than the number of bits in the guid (128). You don't need to hash the guid or anything like that.
EDIT:
Now that I know what you need (i.e. a unique seed to be derived from a guid, you could do it this way) - but you could equally hand out a 32-bit number and avoid the guid-to-int conversion.
EDIT2: Using GetHashCode as per suggestion from comments above.
EDIT 3: Producing unique numbers.
static void Main(string[] args)
{
var guid = new Guid("bdc39e63-5947-4704-9e12-ec66c8773742");
Console.WriteLine(guid);
var numbers = FindNumbersFromGuid(guid, 16, 8);
Console.WriteLine("Numbers: ");
foreach (var elem in numbers)
{
Console.WriteLine(elem);
}
Console.ReadKey();
}
private static int[] FindNumbersFromGuid(Guid input,
int maxNumber, int numberCount)
{
if (numberCount > maxNumber / 2) throw new ArgumentException("Choosing too many numbers.");
var seed = input.GetHashCode();
var random = new Random(seed);
var chosenSoFar = new HashSet<int>();
return Enumerable.Range(0, numberCount)
.Select(e =>
{
var ret = random.Next(0, maxNumber);
while (chosenSoFar.Contains(ret))
{
ret = random.Next(0, maxNumber);
}
chosenSoFar.Add(ret);
return ret;
}).ToArray();
}

Filling an empty object array with a full one randomly in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So lets say i have two object arrays...
Test[] Filled = new Test[5]; //copied from
Test[] Empty = new Test[5]; // pasted to
//they contain 3 different variables aswell
How would you do it if you would copy (not remove) 5 random objects into random positions. From Filled to Empty. There can be duplicates just as long as its randomly done.
This is a possible solution.
Test[] Filled = new Test[5];
Test[] Empty = new Test[5];
//insert some data to Filled array
Random random = new Random();
int randomNumber = random.Next(0, 4);
for (int i = 0; i < Empty.Length; i++)
{
Empty[i] = Filled[randomNumber];
randomNumber = random.Next(0, 4);
}
I assume you mean shuffling with the possiblity of duplicates. Try something like:
var random = new Random();
var randomized = new Test[Filled.Length];
for (int i = 0; i < Filled.Length; i++)
{
randomized[i] = Filled[random.Next(Filled.Length)];
}

Grid of numbers that randomizes on click [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a game that involves finding and clicking on a number (0-9) in a grid that randomizes each time you click on the correct one.
I want to get it so that when you click on the correct number, the grid randomizes again.
How would you do this?
Here's what it would kind of look like in the end:
I assume you're rendering an array of integers in order:
for (int i = 0; i < arrayOfNumbers.Length; i++ ) {
// rendering here
render(arrayOfNumbers[i]);
}
If thats the case.. just randomize the array after a successful click.. somewhat like this:
var rnd = new System.Random();
var arrayOfNumbers = Enumerable.Range(1, 9).OrderBy(r => rnd.Next()).ToArray();
Then you can just re-render (or let your game loop continue to render the array). Since the array has changed, your rendering will too.
Every time you detect a click on the correct number (I hope you know how to do this) you simply randomize the array of numbers you're displaying in your grid:
//Fisher-Yates algorithm
Random generator = new System.Random();
int len = array.Length;
while (len > 1)
{
len--;
int k = generator.Next(len + 1);
int temp = array[k];
array[k] = array[len];
array[len] = temp;
}

Categories