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"
Related
This question already has answers here:
Reverse an array without using Array.Reverse()
(25 answers)
Closed 4 years ago.
I tried this and the output is 5 4 3 4 5 instead of 5 4 3 2 1.
for (int i = 0; i < numbers.Length;i=i+1)
{
int tmp = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1 ];
numbers[numbers.Length - i - 1 ] = tmp;
Console.WriteLine(numbers[i]);
My first question would be why not just use Array.Reverse. But in any case, your bug is in the exit condition of your for loop. You need to stop at numbers.Length/2 or you just swap everything back.
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];
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.
This question already has answers here:
How to split a number into individual digits in c#? [duplicate]
(6 answers)
Closed 8 years ago.
I have a long number that I want to convert to a list of integers corresponding to decimal digits.
long l = 9876543210L;
List<int> list = // how?
Expecetd result: [0,1,2,3,4,5,6,7,8,9] so list[0] will be 9, list[1] will be 8 (2nd digit from the left), etc.
If you're happy to do this via string processing, you could use:
long l = ...;
var list = l.ToString().Select(c => int.Parse(c.ToString())).ToList();
This converts the number to a string (in decimal), then parses each character in the string as an integer.
If performance is critical, you're better off doing this numerically.
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)