Randomly rearrange array items c# [duplicate] - c#

This question already has answers here:
Best way to randomize an array with .NET
(19 answers)
Closed 6 years ago.
I have a c# int array that contains numbers from 1 to 100
that means that
myArray[0] = 1;
myArray[1] = 2;
....
myArray[99] = 100;
But I want to rearrange them in this array randomly, is it possible in c# ?

Using Random and Linq, you can do it easily:
Random r = new Random();
myArray = myArray.OrderBy(x => r.Next()).ToArray();
The above provides a random sort order for each element in the array using a Random object.
You need to add using System.Linq; at the top of your file in order to use OrderBy extension method.

Related

How can i find the highest number number from a list of numbers in an array? [duplicate]

This question already has answers here:
Sort an int array with orderby
(3 answers)
Largest and smallest number in an array
(11 answers)
Closed 5 years ago.
How can i find the highest number from a list of numbers in an array; so far wrote
int[] data = Utilitybook.Utility.fetchData("book01");
float result=0;
Use linq to sort your data:
data = data.OrderByDescending(c => c).ToArray();
Then just get your highest and second highest number from it:
int highest = data[0];
int second = data[1];

How to choose different random number each time in C# [duplicate]

This question already has answers here:
Unique number in random?
(5 answers)
Random number generator with no duplicates
(12 answers)
Closed 5 years ago.
If I have an array that contains some values. How to get different random number each time - which represents an indexes - in which to get different values from the array ?
Generate the sequence of the range you need, and sort it randomly. Then, put the values in a stack and pick from that stack. For example:
var rnd = new Random();
var randomValues = new Stack<int>(Enumerable.Range(0,5).OrderBy(x => rnd.Next()));
var randomIndex1 = randomValues.Pop();
var randomIndex2 = randomValues.Pop();

How to replace all items in an array of doubles with the same value [duplicate]

This question already has answers here:
How to populate/instantiate a C# array with a single value?
(26 answers)
Closed 6 years ago.
So lets say we have an array of doubles like this one that is later used for other stuff.
double[] myArray = new double[25];
How would I go about replacing all the values in that array with a set value?
There are flashier ways, but
for (int i = 0; i < myArray.Length; ++i){
myArray[i] = foo; /*the new value*/
}
is clear and simple.
The "flashy" way (simply create a new array):
var array = Enumerable.Repeat(value, count).ToArray();
Or
Array.ConvertAll(array, e => value);

How do I shuffle a string in Unity C# [duplicate]

This question already has answers here:
Randomizing a string [duplicate]
(2 answers)
Closed 8 years ago.
Is there a simple way in Unity C# to shuffle the alphabet letters in a string? This is to create a shuffled version of a string.
Google Fisher-Yates and you'll find this.
To shuffle an array a of n elements (indices 0..n-1):
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
That should be easy enough to translate into c#
order your string by something unpredictable
var oldString = "shuffle";
var shuffled = new string(oldString.OrderBy(x => Guid.NewGuid()).ToArray());
//shuffled = "feflhus"

Quickest way to generate a sequence of consecutive numbers in a random order [duplicate]

This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 9 years ago.
Whats the quickest way to generate a list of consecutive numbers in a random order?
i.e. generate a list of numbers from 1 to 100, the list must contain each number once only. The order of the list should be random.
java or c# please.
my pseudocode looks like this, very inefficient.
var list = new list<int>();
for (int i = 1; i <= 100; ++i) {
int x;
repeat {
x = random(1, 100);
until (list.contains(x) == false);
list.add(x);
}
Yes, it's teribly inefficient and it's not even bounded in time.
The usual solution is
generate an ordered array
shuffle it (I'd recommend the Fisher–Yates shuffle, it's simple, fast, unbiased and you'll find an implementation in any language)

Categories