Generate Two Arrays of Non-Duplicate Numbers - c#

I have the following code which generates two non-duplicates arrays of integers based on a ratio. The codes works perfectly but for a 4000 line file, this takes some time.
//Train & Test numbers
int train = (int)(((double)Settings.Default.TrainingRatio / 100) * inputLines.Count());
int test = inputLines.Count() - train;
//Train & Test list
Random rnd = new Random();
var trainList = Enumerable.Range(1, inputLines.Count()).OrderBy(x => rnd.Next()).Take(train).ToList();
var testList = new List<int>();
for (int i = 1; i <= inputLines.Count(); i++)
{
if (!trainList.Contains(i))
testList.Add(i);
}
And even worse, this is how i read those lines:
foreach (var n in trainList)
{
objDataintilizer.GenerateMasterLableFile(inputLines.Skip(n - 1).Take(1).First().ToString());
}
Could anyone advice another way that could have a better performance.

Each time your code calls inputFiles.Count(), you're effectively re-reading the entire file, since File.ReadLines is using deferred execution, and you aren't materializing it. Since you need the entire list in-memory anyway, use File.ReadAllLines instead, which returns a string[] and has a Length property, which is an O(1) operation instead of O(N).
Then, instead of using a List<int> for your trainList, use a HashSet<int> which will be faster to lookup with Contains:
public static class EnumerableExtensions
{
public static HashSet<T> ToHashSet(this IEnumerable<T> enumerable)
{
return new HashSet<T>(enumerable);
}
}
Random rnd = new Random();
var trainList = Enumerable.Range(1, inputLines.Length)
.OrderBy(x => rnd.Next())
.Take(train)
.ToHashSet();
var testList = new List<int>();
for (int i = 1; i <= inputLines.Length; i++)
{
if (!trainList.Contains(i))
testList.Add(i);
}

Related

Shuffle Array and make sure elements are not repeating [duplicate]

What is the best way to randomize the order of a generic list in C#? I've got a finite set of 75 numbers in a list I would like to assign a random order to, in order to draw them for a lottery type application.
Shuffle any (I)List with an extension method based on the Fisher-Yates shuffle:
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
Usage:
List<Product> products = GetProducts();
products.Shuffle();
The code above uses the much criticised System.Random method to select swap candidates. It's fast but not as random as it should be. If you need a better quality of randomness in your shuffles use the random number generator in System.Security.Cryptography like so:
using System.Security.Cryptography;
...
public static void Shuffle<T>(this IList<T> list)
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
byte[] box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
A simple comparison is available at this blog (WayBack Machine).
Edit: Since writing this answer a couple years back, many people have commented or written to me, to point out the big silly flaw in my comparison. They are of course right. There's nothing wrong with System.Random if it's used in the way it was intended. In my first example above, I instantiate the rng variable inside of the Shuffle method, which is asking for trouble if the method is going to be called repeatedly. Below is a fixed, full example based on a really useful comment received today from #weston here on SO.
Program.cs:
using System;
using System.Collections.Generic;
using System.Threading;
namespace SimpleLottery
{
class Program
{
private static void Main(string[] args)
{
var numbers = new List<int>(Enumerable.Range(1, 75));
numbers.Shuffle();
Console.WriteLine("The winning numbers are: {0}", string.Join(", ", numbers.GetRange(0, 5)));
}
}
public static class ThreadSafeRandom
{
[ThreadStatic] private static Random Local;
public static Random ThisThreadsRandom
{
get { return Local ?? (Local = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId))); }
}
}
static class MyExtensions
{
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = ThreadSafeRandom.ThisThreadsRandom.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}
If we only need to shuffle items in a completely random order (just to mix the items in a list), I prefer this simple yet effective code that orders items by guid...
var shuffledcards = cards.OrderBy(a => Guid.NewGuid()).ToList();
As people have pointed out in the comments, GUIDs are not guaranteed to be random, so we should be using a real random number generator instead:
private static Random rng = new Random();
...
var shuffledcards = cards.OrderBy(a => rng.Next()).ToList();
I'm bit surprised by all the clunky versions of this simple algorithm here. Fisher-Yates (or Knuth shuffle) is bit tricky but very compact. Why is it tricky? Because your need to pay attention to whether your random number generator r(a,b) returns value where b is inclusive or exclusive. I've also edited Wikipedia description so people don't blindly follow pseudocode there and create hard to detect bugs. For .Net, Random.Next(a,b) returns number exclusive of b so without further ado, here's how it can be implemented in C#/.Net:
public static void Shuffle<T>(this IList<T> list, Random rnd)
{
for(var i=list.Count; i > 0; i--)
list.Swap(0, rnd.Next(0, i));
}
public static void Swap<T>(this IList<T> list, int i, int j)
{
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
Try this code.
Extension method for IEnumerable:
public static IEnumerable<T> Randomize<T>(this IEnumerable<T> source)
{
Random rnd = new Random();
return source.OrderBy<T, int>((item) => rnd.Next());
}
Idea is get anonymous object with item and random order and then reorder items by this order and return value:
var result = items.Select(x => new { value = x, order = rnd.Next() })
.OrderBy(x => x.order).Select(x => x.value).ToList()
public static List<T> Randomize<T>(List<T> list)
{
List<T> randomizedList = new List<T>();
Random rnd = new Random();
while (list.Count > 0)
{
int index = rnd.Next(0, list.Count); //pick a random item from the master list
randomizedList.Add(list[index]); //place it at the end of the randomized list
list.RemoveAt(index);
}
return randomizedList;
}
EDIT
The RemoveAt is a weakness in my previous version. This solution overcomes that.
public static IEnumerable<T> Shuffle<T>(
this IEnumerable<T> source,
Random generator = null)
{
if (generator == null)
{
generator = new Random();
}
var elements = source.ToArray();
for (var i = elements.Length - 1; i >= 0; i--)
{
var swapIndex = generator.Next(i + 1);
yield return elements[swapIndex];
elements[swapIndex] = elements[i];
}
}
Note the optional Random generator, if the base framework implementation of Random is not thread-safe or cryptographically strong enough for your needs, you can inject your implementation into the operation.
A suitable implementation for a thread-safe cryptographically strong Random implementation can be found in this answer.
Here's an idea, extend IList in a (hopefully) efficient way.
public static IEnumerable<T> Shuffle<T>(this IList<T> list)
{
var choices = Enumerable.Range(0, list.Count).ToList();
var rng = new Random();
for(int n = choices.Count; n > 1; n--)
{
int k = rng.Next(n);
yield return list[choices[k]];
choices.RemoveAt(k);
}
yield return list[choices[0]];
}
This is my preferred method of a shuffle when it's desirable to not modify the original. It's a variant of the Fisher–Yates "inside-out" algorithm that works on any enumerable sequence (the length of source does not need to be known from start).
public static IList<T> NextList<T>(this Random r, IEnumerable<T> source)
{
var list = new List<T>();
foreach (var item in source)
{
var i = r.Next(list.Count + 1);
if (i == list.Count)
{
list.Add(item);
}
else
{
var temp = list[i];
list[i] = item;
list.Add(temp);
}
}
return list;
}
This algorithm can also be implemented by allocating a range from 0 to length - 1 and randomly exhausting the indices by swapping the randomly chosen index with the last index until all indices have been chosen exactly once. This above code accomplishes the exact same thing but without the additional allocation. Which is pretty neat.
With regards to the Random class it's a general purpose number generator (and If I was running a lottery I'd consider using something different). It also relies on a time based seed value by default. A small alleviation of the problem is to seed the Random class with the RNGCryptoServiceProvider or you could use the RNGCryptoServiceProvider in a method similar to this (see below) to generate uniformly chosen random double floating point values but running a lottery pretty much requires understanding randomness and the nature of the randomness source.
var bytes = new byte[8];
_secureRng.GetBytes(bytes);
var v = BitConverter.ToUInt64(bytes, 0);
return (double)v / ((double)ulong.MaxValue + 1);
The point of generating a random double (between 0 and 1 exclusively) is to use to scale to an integer solution. If you need to pick something from a list based on a random double x that's always going to be 0 <= x && x < 1 is straight forward.
return list[(int)(x * list.Count)];
Enjoy!
If you don't mind using two Lists, then this is probably the easiest way to do it, but probably not the most efficient or unpredictable one:
List<int> xList = new List<int>() { 1, 2, 3, 4, 5 };
List<int> deck = new List<int>();
foreach (int xInt in xList)
deck.Insert(random.Next(0, deck.Count + 1), xInt);
I usually use:
var list = new List<T> ();
fillList (list);
var randomizedList = new List<T> ();
var rnd = new Random ();
while (list.Count != 0)
{
var index = rnd.Next (0, list.Count);
randomizedList.Add (list [index]);
list.RemoveAt (index);
}
You can achieve that be using this simple extension method
public static class IEnumerableExtensions
{
public static IEnumerable<t> Randomize<t>(this IEnumerable<t> target)
{
Random r = new Random();
return target.OrderBy(x=>(r.Next()));
}
}
and you can use it by doing the following
// use this on any collection that implements IEnumerable!
// List, Array, HashSet, Collection, etc
List<string> myList = new List<string> { "hello", "random", "world", "foo", "bar", "bat", "baz" };
foreach (string s in myList.Randomize())
{
Console.WriteLine(s);
}
Just wanted to suggest a variant using an IComparer<T> and List.Sort():
public class RandomIntComparer : IComparer<int>
{
private readonly Random _random = new Random();
public int Compare(int x, int y)
{
return _random.Next(-1, 2);
}
}
Usage:
list.Sort(new RandomIntComparer());
One can use the Shuffle extension methond from morelinq package, it works on IEnumerables
install-package morelinq
using MoreLinq;
...
var randomized = list.Shuffle();
If you have a fixed number (75), you could create an array with 75 elements, then enumerate your list, moving the elements to randomized positions in the array. You can generate the mapping of list number to array index using the Fisher-Yates shuffle.
You can make the Fisher-Yates shuffle more terse and expressive by using tuples for the swap.
private static readonly Random random = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = random.Next(n + 1);
(list[k], list[n]) = (list[n], list[k]);
}
}
I have found an interesting solution online.
Courtesy: https://improveandrepeat.com/2018/08/a-simple-way-to-shuffle-your-lists-in-c/
var shuffled = myList.OrderBy(x => Guid.NewGuid()).ToList();
We can use an extension method for List and use a thread-safe random generator combination. I've packaged an improved version of this on NuGet with the source code available on GitHub. The NuGet version contains optional cryptographically-strong random.
Pre-.NET 6.0 version:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Shuffle<T>(this IList<T> list)
{
if (list == null) throw new ArgumentNullException(nameof(list));
int n = list.Count;
while (n > 1)
{
int k = ThreadSafeRandom.Instance.Next(n--);
(list[n], list[k]) = (list[k], list[n]);
}
}
internal class ThreadSafeRandom
{
public static Random Instance => _local.Value;
private static readonly Random _global = new Random();
private static readonly ThreadLocal<Random> _local = new ThreadLocal<Random>(() =>
{
int seed;
lock (_global)
{
seed = _global.Next();
}
return new Random(seed);
});
}
On .NET 6.0 or later:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Shuffle<T>(this IList<T> list)
{
ArgumentNullException.ThrowIfNull(list);
int n = list.Count;
while (n > 1)
{
int k = Random.Shared.Next(n--);
(list[n], list[k]) = (list[k], list[n]);
}
}
Install the library via NuGet for more features.
A simple modification of the accepted answer that returns a new list instead of working in-place, and accepts the more general IEnumerable<T> as many other Linq methods do.
private static Random rng = new Random();
/// <summary>
/// Returns a new list where the elements are randomly shuffled.
/// Based on the Fisher-Yates shuffle, which has O(n) complexity.
/// </summary>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list) {
var source = list.ToList();
int n = source.Count;
var shuffled = new List<T>(n);
shuffled.AddRange(source);
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = shuffled[k];
shuffled[k] = shuffled[n];
shuffled[n] = value;
}
return shuffled;
}
List<T> OriginalList = new List<T>();
List<T> TempList = new List<T>();
Random random = new Random();
int length = OriginalList.Count;
int TempIndex = 0;
while (length > 0) {
TempIndex = random.Next(0, length); // get random value between 0 and original length
TempList.Add(OriginalList[TempIndex]); // add to temp list
OriginalList.RemoveAt(TempIndex); // remove from original list
length = OriginalList.Count; // get new list <T> length.
}
OriginalList = new List<T>();
OriginalList = TempList; // copy all items from temp list to original list.
Here is an implementation of the Fisher-Yates shuffle that allows specification of the number of elements to return; hence, it is not necessary to first sort the whole collection before taking your desired number of elements.
The sequence of swapping elements is reversed from default; and proceeds from the first element to the last element, so that retrieving a subset of the collection yields the same (partial) sequence as shuffling the whole collection:
collection.TakeRandom(5).SequenceEqual(collection.Shuffle().Take(5)); // true
This algorithm is based on Durstenfeld's (modern) version of the Fisher-Yates shuffle on Wikipedia.
public static IList<T> TakeRandom<T>(this IEnumerable<T> collection, int count, Random random) => shuffle(collection, count, random);
public static IList<T> Shuffle<T>(this IEnumerable<T> collection, Random random) => shuffle(collection, null, random);
private static IList<T> shuffle<T>(IEnumerable<T> collection, int? take, Random random)
{
var a = collection.ToArray();
var n = a.Length;
if (take <= 0 || take > n) throw new ArgumentException("Invalid number of elements to return.");
var end = take ?? n;
for (int i = 0; i < end; i++)
{
var j = random.Next(i, n);
(a[i], a[j]) = (a[j], a[i]);
}
if (take.HasValue) return new ArraySegment<T>(a, 0, take.Value);
return a;
}
Implementation:
public static class ListExtensions
{
public static void Shuffle<T>(this IList<T> list, Random random)
{
for (var i = list.Count - 1; i > 0; i--)
{
int indexToSwap = random.Next(i + 1);
(list[indexToSwap], list[i]) = (list[i], list[indexToSwap]);
}
}
}
Example:
var random = new Random();
var array = new [] { 1, 2, 3 };
array.Shuffle(random);
foreach (var item in array) {
Console.WriteLine(item);
}
Demonstration in .NET Fiddle
Here's an efficient Shuffler that returns a byte array of shuffled values. It never shuffles more than is needed. It can be restarted from where it previously left off. My actual implementation (not shown) is a MEF component that allows a user specified replacement shuffler.
public byte[] Shuffle(byte[] array, int start, int count)
{
int n = array.Length - start;
byte[] shuffled = new byte[count];
for(int i = 0; i < count; i++, start++)
{
int k = UniformRandomGenerator.Next(n--) + start;
shuffled[i] = array[k];
array[k] = array[start];
array[start] = shuffled[i];
}
return shuffled;
}
`
Your question is how to randomize a list. This means:
All unique combinations should be possible of happening
All unique combinations should occur with the same distribution (AKA being non-biased).
A large number of the answers posted for this question do NOT satisfy the two requirements above for being "random".
Here's a compact, non-biased pseudo-random function following the Fisher-Yates shuffle method.
public static void Shuffle<T>(this IList<T> list, Random rnd)
{
for (var i = list.Count-1; i > 0; i--)
{
var randomIndex = rnd.Next(i + 1); //maxValue (i + 1) is EXCLUSIVE
list.Swap(i, randomIndex);
}
}
public static void Swap<T>(this IList<T> list, int indexA, int indexB)
{
var temp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = temp;
}
Here's a thread-safe way to do this:
public static class EnumerableExtension
{
private static Random globalRng = new Random();
[ThreadStatic]
private static Random _rng;
private static Random rng
{
get
{
if (_rng == null)
{
int seed;
lock (globalRng)
{
seed = globalRng.Next();
}
_rng = new Random(seed);
}
return _rng;
}
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> items)
{
return items.OrderBy (i => rng.Next());
}
}
public Deck(IEnumerable<Card> initialCards)
{
cards = new List<Card>(initialCards);
public void Shuffle()
}
{
List<Card> NewCards = new List<Card>();
while (cards.Count > 0)
{
int CardToMove = random.Next(cards.Count);
NewCards.Add(cards[CardToMove]);
cards.RemoveAt(CardToMove);
}
cards = NewCards;
}
public IEnumerable<string> GetCardNames()
{
string[] CardNames = new string[cards.Count];
for (int i = 0; i < cards.Count; i++)
CardNames[i] = cards[i].Name;
return CardNames;
}
Deck deck1;
Deck deck2;
Random random = new Random();
public Form1()
{
InitializeComponent();
ResetDeck(1);
ResetDeck(2);
RedrawDeck(1);
RedrawDeck(2);
}
private void ResetDeck(int deckNumber)
{
if (deckNumber == 1)
{
int numberOfCards = random.Next(1, 11);
deck1 = new Deck(new Card[] { });
for (int i = 0; i < numberOfCards; i++)
deck1.Add(new Card((Suits)random.Next(4),(Values)random.Next(1, 14)));
deck1.Sort();
}
else
deck2 = new Deck();
}
private void reset1_Click(object sender, EventArgs e) {
ResetDeck(1);
RedrawDeck(1);
}
private void shuffle1_Click(object sender, EventArgs e)
{
deck1.Shuffle();
RedrawDeck(1);
}
private void moveToDeck1_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex >= 0)
if (deck2.Count > 0) {
deck1.Add(deck2.Deal(listBox2.SelectedIndex));
}
RedrawDeck(1);
RedrawDeck(2);
}
private List<GameObject> ShuffleList(List<GameObject> ActualList) {
List<GameObject> newList = ActualList;
List<GameObject> outList = new List<GameObject>();
int count = newList.Count;
while (newList.Count > 0) {
int rando = Random.Range(0, newList.Count);
outList.Add(newList[rando]);
newList.RemoveAt(rando);
}
return (outList);
}
usage :
List<GameObject> GetShuffle = ShuffleList(ActualList);
Old post for sure, but I just use a GUID.
Items = Items.OrderBy(o => Guid.NewGuid().ToString()).ToList();
A GUID is always unique, and since it is regenerated every time the result changes each time.
A very simple approach to this kind of problem is to use a number of random element swap in the list.
In pseudo-code this would look like this:
do
r1 = randomPositionInList()
r2 = randomPositionInList()
swap elements at index r1 and index r2
for a certain number of times

Is there a way to make the Loop Code make it faster?

For Loop Code
int counts = 0;
List<int> count = new List<int>();
List<int> goodnumber = new List<int>();
for (int i = lower; i <= upper; i++)
{
if (!badNumbers.Contains(i)) {
goodnumber.Add(i);
} else {
count.Add(goodnumber.Count);
goodnumber = new List<int>();
}
if (i == upper) {
count.Add(goodnumber.Count);
counts = count.Max();
}
}
return counts;
is there a way to optimize my code above? because the running time for the code above is exceeding in 3 secs. how can I make it 2 or below?
There's a few improvements you can make.
badNumbers should probably be a HashSet<int> which will provide you close to O(1) lookup.
You don't actually care about storing the "good numbers" (you don't use that data), so it would be more efficient to just store how many good numbers you encounter.
Now you just want the max streak size (i.e. max number of consecutive good numbers) you encounter, and you can use Math.Max to compare the last "good" count with the current "good" count and choose the largest.
The code looks like this:
HashSet<int> badNumbers = new HashSet<int>() { 5, 4, 2, 15 };
int counts = 0;
int goodNumberCount = 0;
for (int i = lower; i <= upper; i++)
{
if (!badNumbers.Contains(i)) {
++goodNumberCount;
} else {
counts = Math.Max(counts, goodNumberCount);
goodNumberCount = 0;
}
}
counts = Math.Max(counts, goodNumberCount);
return counts;
Call List.Clear() instead of creating new List inside the loop
Call count.Max() outside the loop
Remove the last if and add this line after the loop count.Add(goodnumber.Count)
int counts = 0;
List<int> count = new List<int>();
List<int> goodnumber = new List<int>();
for (int i = lower; i <= upper; i++)
{
if (!badNumbers.Contains(i)) {
goodnumber.Add(i);
} else {
count.Add(goodnumber.Count);
goodnumber.Clear();
}
}
count.Add(goodnumber.Count);
counts = count.Max();
return counts;
BTW, I don't know what are you trying to achieve with this code.
The correct way to "optimize" your code is to rewrite it. You need to think differently. The problem you have has various different solutions and you are complicating it too much.
You don't need to process the input in one long cycle only. You can pre-process the list somehow, in a way, that would help you. For example sort it.
Another thing that could help you is to have a variable (or variables) in which you are storing some intermediate result. For example running max, min, sum, or previous value of something
Think about how you could solve the problem mathematically. Isn't it just the difference of numbers you are trying to find?
You could sort the list, calculate the difference between each element, bound it by your lower and upper borders. You can either update the running maximum difference during the loop or find the maximum difference from the list of differences.
Here is a general solution:
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var lower = 1;
var upper = 10;
var elementCount = upper - lower + 1;
var numbers = Enumerable.Range(1, elementCount);
var badNumbers = new HashSet<int> { 5, 4, 2, 15 };
var maxCount = CalculateCounts(numbers, badNumbers).Max();
}
private static IEnumerable<int> CalculateCounts<T>(IEnumerable<T> items, ISet<T> splitOn)
{
var count = 0;
foreach (var item in items)
{
if (!splitOn.Contains(item)) count++;
else
{
yield return count;
count = 0;
}
}
yield return count;
}
}
}

get random 8 element values from array string without duplicated element

I have a string array with length is 100.
I want to get random 8 elements among 100 element without duplicated elements in C#.
Please help me.
I appreciate it.
I just use:
for (int i = 0; i < 8; i++)
{
work with 8 values here
}
Above code just performs to get 8 first values not 8 random values.
Easy way:
var random = new Random();
var randomValues = arr.OrderBy(x => random.Next()).Take(8)
Effective way: use Fisher–Yates shuffle. Jon Skeet provided the implementation here.
Here is the code.
Explanation
Randomly generate numbers between 0 - 99.
Using a hashset to keep track of indices that I am picking. If the index is present in hashset then it means that the value has already been selected, so skip it.
If the values in array are not unique, then in hashset instead of tracking index track values.
public List<string> GetRandomElements(string[] givenArray)
{
if(givenArray == null || givenArray.Length == 0)
{
throw new ArgumentNullException("givenArray");
}
var rand = new Random();
var randomArray = new List<string>();
var indexTracker = new HashSet<int>();
while(randomArray.Count < 8)
{
var nextIndex = rand.Next(0, givenArray.Length);
if(indexTracker.Contains(nextIndex))
{
continue;
}
randomArray.Add(givenArray[nextIndex]);
indexTracker.Add(nextIndex);
}
return randomArray;
}
Note I assumed here there was no constraint on using extra memory for hashset (because there will be max of 8 members only). If that is the constraint, then another way could be, split the index into 8 parts. So pick first index between 0-8, next index between 9-16 and so one. This way you will get unique numbers.
You can use generics and simply do:
static T[] GetRandomRange<T>(T[] arr, int length)
{
var r = new Random();
List<T> elementsList = new List<T>();
for (; elementsList.Count < length; )
{
T el = arr[r.Next(0, arr.Length)];
if (!elementsList.Contains(el)) elementsList.Add(el);
}
return elementsList.ToArray();
}
Use it like :
int[] arr ...
int[] newArray = GetRandomRange(arr, 8);
MyClass[] arr2...
MyClass[] newArray2 = GetRandomRange(arr2, 5);
It's pretty straightforward, just pass it to HashSet<string> and apply the following logic:
var mySet = new HashSet<string>(myList);
var myList = new List<string>();
var random = new Random();
for (int i = 0; i < 8; i++)
{
int randValue = random.Next(0, mySet.Count());
string randSetValue = mySet.ElementAt(randValue);
myList.Add(randSetValue );
mySet.Remove(randSetValue);
}

Initialize a Jagged Array the LINQ Way

I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using the traditional loop:
var myArr = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
myArr[i] = new double[colCount];
}
I thought maybe some LINQ function would give me an elegant way to do this in one statement. However, the closest I can come up with is this:
double[][] myArr = Enumerable.Repeat(new double[colCount], rowCount).ToArray();
The problem is that it seems to be creating a single double[colCount] and assigning references to that intsead of allocating a new array for each row. Is there a way to do this without getting too cryptic?
double[][] myArr = Enumerable
.Range(0, rowCount)
.Select(i => new double[colCount])
.ToArray();
What you have won't work as the new occurs before the call to Repeat. You need something that also repeats the creation of the array. This can be achieved using the Enumerable.Range method to generate a range and then performing a Select operation that maps each element of the range to a new array instance (as in Amy B's answer).
However, I think that you are trying to use LINQ where it isn't really appropriate to do so in this case. What you had prior to the LINQ solution is just fine. Of course, if you wanted a LINQ-style approach similar to Enumerable.Repeat, you could write your own extension method that generates a new item, such as:
public static IEnumerable<TResult> Repeat<TResult>(
Func<TResult> generator,
int count)
{
for (int i = 0; i < count; i++)
{
yield return generator();
}
}
Then you can call it as follows:
var result = Repeat(()=>new double[rowCount], columnCount).ToArray();
The behavior is correct - Repeat() returns a sequence that contains the supplied object multiple times. You can do the following trick.
double[][] myArr = Enumerable
.Repeat(0, rowCount)
.Select(i => new double[colCount])
.ToArray();
You can't do that with the Repeat method : the element parameter is only evaluated once, so indeed it always repeats the same instance. Instead, you could create a method to do what you want, which would take a lambda instead of a value :
public static IEnumerable<T> Sequence<T>(Func<T> generator, int count)
{
for (int i = 0; i < count; i++)
{
yield return generator();
}
}
...
var myArr = Sequence(() => new double[colCount], rowCount).ToArray();
I just wrote this function...
public static T[][] GetMatrix<T>(int m, int n)
{
var v = new T[m][];
for(int i=0;i<m; ++i) v[i] = new T[n];
return v;
}
Seems to work.
Usage:
float[][] vertices = GetMatrix<float>(8, 3);
What about
var myArr = new double[rowCount, colCount];
or
double myArr = new double[rowCount, colCount];
Reference: http://msdn.microsoft.com/en-us/library/aa691346(v=vs.71).aspx

What is the easiest way to get 4(or any count) random unique ids from an id collection with c#?

Assume that you have an idCollection IList<long> and you have a method to get 4 unique ids.Every time you call it, it gives you random 4 unique ids ?
var idCollec = new[] {1,2,3,4,5,6,7,8,9,10,11,12}.ToList();
For example {2,6,11,12}
{3,4,7,8}
{5,8,10,12}
...
..
What is the smartest way to do it ?
Thanks
Seems like easiest way would be to have something like:
if(idCollection.Count <4)
{
throw new ArgumentException("Source array not long enough");
}
List<long> FourUniqueIds = new List<long>(4);
while(FourUniqueIds.Count <4)
{
long temp = idCollection[random.Next(idCollection.Count)];
if(!FourUniqueIds.Contains(temp))
{
FourUniqueIds.add(temp);
}
}
It can be done with a nice LINQ query. The key to doing it without the risk of getting duplicates, is to create a never ending IEnumerable of random integers. Then you can take n distinct values from it, and use them as indexes into the list.
Sample program:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestRandom
{
class Program
{
static void Main(string[] args)
{
// Just to prepopulate a list.
var ids = (from n in Enumerable.Range(0, 100)
select (long)rand.Next(0, 1000)).ToList();
// Example usage of the GetRandomSet method.
foreach(long id in GetRandomSet(ids, 4))
Console.WriteLine(id);
}
// Get count random entries from the list.
public static IEnumerable<long> GetRandomSet(IList<long> ids, int count)
{
// Can't get more than there is in the list.
if ( count > ids.Count)
count = ids.Count;
return RandomIntegers(0, ids.Count)
.Distinct()
.Take(count)
.Select(index => ids[index]);
}
private static IEnumerable<int> RandomIntegers(int min, int max)
{
while (true)
yield return rand.Next(min, max);
}
private static readonly Random rand = new Random();
}
}
If you use this approach, make sure you do not try to take more distinct values than there are available in the range passed to RandomIntegers.
What about shuffling the set then just taking the first four each time?
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> e)
{
var r = new Random();
return e.OrderBy(x => r.Next());
}
Then something like this? It would probably be faster to use a for loop instead of Take and Except.
var ordered = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 10};
var random = ordered.Shuffle();
while(random.Count() > 0)
{
var ourSet = random.Take(4).ToList();
random = random.Except(ourSet);
}
If the IList<long> is populated with non unique values, you could use LINQ's Distinct() in combination with Take(), if it already has unique values, just use Take().
List<long> myUniqueIds = //prepoulation
var first4UniqueUnused = myUniqueIds.Take(4);
var next4UniqueUnused = myUniqueIds.Where(l=>!first4UniqueUnused.Contains(l)).Take(4);
another way that is too easy, i think we've been making it too hard:
List<long> myIDs = //prepopulation;
List<long> my4Random = new List<long>();
Random r = new Random();
for(int i=0; i< 4; i++)
{
int j = r.Next();
while(j>myIDs.Count || my4Random.Contains(myIDs[j]))
j = r.Next();
my4Random.Add(myIDs[j]);
}
Random random = new Random();
long firstOne = idCollection[random.Next(idCollection.Count)];
long secondOne = idCollection[random.NExt(idCollection.Count)];
...and so on

Categories

Resources