get random 8 element values from array string without duplicated element - c#

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);
}

Related

Eliminate duplicates from array c#

I'm making a basic Deal or No Deal game, in doing so I have to pick 10 finalists from an array, at random, without repeats.
I have my structure and arrays set out like this
public struct People
{
public string firstname;
public string lastname;
public int age;
}
class Program
{
public static People[] People1 = new People[40];
public static People[] Finalists1 = new People[10];
public static People[] Finalist1 = new People[1];
And my finalists method set out like this
Random rand = new Random();
for (int i = 0; i < Finalists1.Length; i++)
{
num = rand.Next(0, People1.Length);
Finalists1[i].lastname = People1[num].lastname;
Finalists1[i].firstname = People1[num].firstname;
Finalists1[i].age = People1[num].age;
}
How can I eliminate duplicate entries, while maintaining 10 people in the array?
Since initial array doesn't contain duplicates, you can sort it in random order and pick up 10 top items:
Finalists1 = People1
.OrderByDescending(item => 1) // if people have some points, bonuses etc.
.ThenBy(item => Guid.NewGuid()) // shuffle among peers
.Take(10) // Take top 10
.ToArray(); // materialize as an array
If people are selected to the final are not completely random (e.g. contestant can earn points, bonuses etc.) change .OrderByDescending(item => 1), e.g.
.OrderByDescending(item => item.Bonuses)
If you don't want to use Linq, you can just draw Peoples from urn without returning:
private static Random random = new Random();
...
List<People> urn = new List<People>(People1);
for (int i = 0; i < Finalists1.Length; ++i) {
int index = random.Next(0, urn.Count);
Finalists1[i] = urn[index];
urn.RemoveAt(index);
}
You can hold a list or hash set of numbers you have already drawn. Then just roll the dice again to get another random number.
Random rand = new Random();
HashSet<int> drawnNumbers = new HashSet<int>();
for (int i = 0; i < Finalists1.Length; i++)
{
do
{
num = rand.Next(0, People1.Length);
}
while (drawnNumbers.Contains(num));
Finalists1[i] = People1[num];
}
You can change the type of Finalists1 to a HashSet, that does not allow duplicates.
Then change your loop to
while(Finalists1.Length < 10)
{
// random pick from array People1 (you don't need to create a new one)
num = rand.Next(0, People1.Length);
var toAdd = People1[num];
// add to hash-set. Object won't be added, if already existing in the set
Finalists1.Add(toAdd);
}
You probably need to override the Equals method of class People, if you really need to create a new object to add to the hash-set.
You can group people array and select distinct that way.
If you use List you can remove person from the list
`var peopleArray = new People[40];
var peopleCollection = peopleArray.GroupBy(p => new { p.age, p.firstname, p.lastname }).Select(grp => grp.FirstOrDefault()).ToList();
var finalists = new People[10];
var rand = new Random();
for (var i = 0; i < finalists.Length; i++)
{
var index = rand.Next(0, peopleCollection.Count);
var person = peopleCollection[index];
finalists[i].lastname = person.lastname;
finalists[i].firstname = person.firstname;
finalists[i].age = person.age;
peopleCollection.Remove(person);
}
shuffle and take the first 10, for example
People1.Shuffle();
Finalists1= People1.Take(10).ToArray();
you can find shuffle code from StackOverflow or search for "Fisher-Yates shuffle C#" Below methods are taken from This SO Post. Read the answers for more information on why GUID is not used etc..
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;
}
}
}
Swap each selected element in People1 to with the end of the array, and decrement an end-of-array index so that you're only selecting from what's left on the next iteration.
People tempPerson = new People;
int lastElem = People1.length - 1;
for (int i = 0; i < Finalists1.Length; i++)
{
num = rand.Next(0, lastElem + 1);
Finalists1[i] = People1[num];
//swap last entry in People1 with People1[num]
tempPerson = People1[num];
People1[num] = People1[lastElem];
People1[lastElem] = tempPerson;
lastElem--;
}
Sorry if there's a syntax error, I'm mostly using Java and C# these days.
BTW You don't have to set the fields individually since each array stores objects of type Person.

Generate Two Arrays of Non-Duplicate Numbers

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);
}

getting out of range exception in bool array(c#)

im trying to use random number to to pull 30 strings out of a array of 58 strings and am using a bool array to check and make sure the same number is not called twice. the method and the program always crashes with a index out of range error. here is the method.
static string[] newlist(string[] s)
{
string[] newlist = {};
bool[] issearched = new bool[s.Length];
Random callorder = new Random();
for (int i = 0; i < 31; i++)
{
int number = callorder.Next(0, s.Length);
if (issearched[number] == false)
{
newlist[number] = s[number];
issearched[number] = true;//this is where it always crashes even though the ide says issearced has 58 elements and the random number is always smaller than that.
}
else
i--;
}
return newlist;
}
im sure its simple but i can't figure out why index of 8 is outside the range of the array of 58.
Your array newlist (what a confusing name) has no space to store anything.
This line
string[] newlist = {};
declares the array but without setting the space to store any element, so when you try to use the indexer on it you get the exception.
I suggest to use a different approach to find 30 strings from your passed array.
Using a List<string> and continue to add to this list until you have 30 elements in the list
static string[] newlist(string[] s)
{
List<string> selectedElements = new List<string>();
bool[] issearched = new bool[s.Length];
Random callorder = new Random();
while(selectedElements.Count < 30))
{
int number = callorder.Next(0, s.Length);
if (!issearched[number])
{
selectedElements.Add(s[number]);
issearched[number] = true;
}
}
return selectedElements.ToArray();
}
If you prefer to use arrays as from your method then a couple of fixing is required to your code
static string[] newlist(string[] s)
{
string[] newlist = new string[30];
bool[] issearched = new bool[s.Length];
Random callorder = new Random();
for (int i = 0; i < 30; i++)
{
int number = callorder.Next(0, s.Length);
if (issearched[number] == false)
{
newlist[i] = s[number];
issearched[number] = true;
}
else
i--;
}
return newlist;
}
The newlist array is declared to have space to store 30 elements
The for loops for 30 times (not 31 as from your current code)
The newlist should use as indexer the value of the variable i
I believe you're actually crashing here:
newlist[number] = s[number];
Replace
string[] newlist = {};
With
string[] newlist = new string[s.Length];
Your newlist size is 0 elements, nowhere are you allocating enough space for it.
Also your program will go into an infinite loop if the input size is less than 31 elements.

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

Adding values to a C# array

Probably a really simple one this - I'm starting out with C# and need to add values to an array, for example:
int[] terms;
for(int runs = 0; runs < 400; runs++)
{
terms[] = runs;
}
For those who have used PHP, here's what I'm trying to do in C#:
$arr = array();
for ($i = 0; $i < 10; $i++) {
$arr[] = $i;
}
You can do this way -
int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
Alternatively, you can use Lists - the advantage with lists being, you don't need to know the array size when instantiating the list.
List<int> termsList = new List<int>();
for (int runs = 0; runs < 400; runs++)
{
termsList.Add(value);
}
// You can convert it back to an array if you would like to
int[] terms = termsList.ToArray();
Edit: a) for loops on List<T> are a bit more than 2 times cheaper than foreach loops on List<T>, b) Looping on array is around 2 times cheaper than looping on List<T>, c) looping on array using for is 5 times cheaper than looping on List<T> using foreach (which most of us do).
Using Linq's method Concat makes this simple
int[] array = new int[] { 3, 4 };
array = array.Concat(new int[] { 2 }).ToArray();
result
3,4,2
If you're writing in C# 3, you can do it with a one-liner:
int[] terms = Enumerable.Range(0, 400).ToArray();
This code snippet assumes that you have a using directive for System.Linq at the top of your file.
On the other hand, if you're looking for something that can be dynamically resized, as it appears is the case for PHP (I've never actually learned it), then you may want to use a List instead of an int[]. Here's what that code would look like:
List<int> terms = Enumerable.Range(0, 400).ToList();
Note, however, that you cannot simply add a 401st element by setting terms[400] to a value. You'd instead need to call Add() like this:
terms.Add(1337);
By 2019 you can use Append, Prepend using LinQ in just one line
using System.Linq;
and then in NET 6.0:
terms = terms.Append(21);
or versions lower than NET 6.0
terms = terms.Append(21).ToArray();
Answers on how to do it using an array are provided here.
However, C# has a very handy thing called System.Collections
Collections are fancy alternatives to using an array, though many of them use an array internally.
For example, C# has a collection called List that functions very similar to the PHP array.
using System.Collections.Generic;
// Create a List, and it can only contain integers.
List<int> list = new List<int>();
for (int i = 0; i < 400; i++)
{
list.Add(i);
}
Using a List as an intermediary is the easiest way, as others have described, but since your input is an array and you don't just want to keep the data in a List, I presume you might be concerned about performance.
The most efficient method is likely allocating a new array and then using Array.Copy or Array.CopyTo. This is not hard if you just want to add an item to the end of the list:
public static T[] Add<T>(this T[] target, T item)
{
if (target == null)
{
//TODO: Return null or throw ArgumentNullException;
}
T[] result = new T[target.Length + 1];
target.CopyTo(result, 0);
result[target.Length] = item;
return result;
}
I can also post code for an Insert extension method that takes a destination index as input, if desired. It's a little more complicated and uses the static method Array.Copy 1-2 times.
Based on the answer of Thracx (I don't have enough points to answer):
public static T[] Add<T>(this T[] target, params T[] items)
{
// Validate the parameters
if (target == null) {
target = new T[] { };
}
if (items== null) {
items = new T[] { };
}
// Join the arrays
T[] result = new T[target.Length + items.Length];
target.CopyTo(result, 0);
items.CopyTo(result, target.Length);
return result;
}
This allows to add more than just one item to the array, or just pass an array as a parameter to join two arrays.
You have to allocate the array first:
int [] terms = new int[400]; // allocate an array of 400 ints
for(int runs = 0; runs < terms.Length; runs++) // Use Length property rather than the 400 magic number again
{
terms[runs] = value;
}
int ArraySize = 400;
int[] terms = new int[ArraySize];
for(int runs = 0; runs < ArraySize; runs++)
{
terms[runs] = runs;
}
That would be how I'd code it.
C# arrays are fixed length and always indexed. Go with Motti's solution:
int [] terms = new int[400];
for(int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
Note that this array is a dense array, a contiguous block of 400 bytes where you can drop things. If you want a dynamically sized array, use a List<int>.
List<int> terms = new List<int>();
for(int runs = 0; runs < 400; runs ++)
{
terms.Add(runs);
}
Neither int[] nor List<int> is an associative array -- that would be a Dictionary<> in C#. Both arrays and lists are dense.
You can't just add an element to an array easily. You can set the element at a given position as fallen888 outlined, but I recommend to use a List<int> or a Collection<int> instead, and use ToArray() if you need it converted into an array.
If you really need an array the following is probly the simplest:
using System.Collections.Generic;
// Create a List, and it can only contain integers.
List<int> list = new List<int>();
for (int i = 0; i < 400; i++)
{
list.Add(i);
}
int [] terms = list.ToArray();
one approach is to fill an array via LINQ
if you want to fill an array with one element
you can simply write
string[] arrayToBeFilled;
arrayToBeFilled= arrayToBeFilled.Append("str").ToArray();
furthermore, If you want to fill an array with multiple elements you can use the
previous code in a loop
//the array you want to fill values in
string[] arrayToBeFilled;
//list of values that you want to fill inside an array
List<string> listToFill = new List<string> { "a1", "a2", "a3" };
//looping through list to start filling the array
foreach (string str in listToFill){
// here are the LINQ extensions
arrayToBeFilled= arrayToBeFilled.Append(str).ToArray();
}
Array Push Example
public void ArrayPush<T>(ref T[] table, object value)
{
Array.Resize(ref table, table.Length + 1); // Resizing the array for the cloned length (+-) (+1)
table.SetValue(value, table.Length - 1); // Setting the value for the new element
}
int[] terms = new int[10]; //create 10 empty index in array terms
//fill value = 400 for every index (run) in the array
//terms.Length is the total length of the array, it is equal to 10 in this case
for (int run = 0; run < terms.Length; run++)
{
terms[run] = 400;
}
//print value from each of the index
for (int run = 0; run < terms.Length; run++)
{
Console.WriteLine("Value in index {0}:\t{1}",run, terms[run]);
}
Console.ReadLine();
/*Output:
Value in index 0: 400
Value in index 1: 400
Value in index 2: 400
Value in index 3: 400
Value in index 4: 400
Value in index 5: 400
Value in index 6: 400
Value in index 7: 400
Value in index 8: 400
Value in index 9: 400
*/
If you don't know the size of the Array or already have an existing array that you are adding to. You can go about this in two ways. The first is using a generic List<T>:
To do this you will want convert the array to a var termsList = terms.ToList(); and use the Add method. Then when done use the var terms = termsList.ToArray(); method to convert back to an array.
var terms = default(int[]);
var termsList = terms == null ? new List<int>() : terms.ToList();
for(var i = 0; i < 400; i++)
termsList.Add(i);
terms = termsList.ToArray();
The second way is resizing the current array:
var terms = default(int[]);
for(var i = 0; i < 400; i++)
{
if(terms == null)
terms = new int[1];
else
Array.Resize<int>(ref terms, terms.Length + 1);
terms[terms.Length - 1] = i;
}
If you are using .NET 3.5 Array.Add(...);
Both of these will allow you to do it dynamically. If you will be adding lots of items then just use a List<T>. If it's just a couple of items then it will have better performance resizing the array. This is because you take more of a hit for creating the List<T> object.
Times in ticks:
3 items
Array Resize Time: 6
List Add Time: 16
400 items
Array Resize Time: 305
List Add Time: 20
I will add this for a another variant. I prefer this type of functional coding lines more.
Enumerable.Range(0, 400).Select(x => x).ToArray();
You can't do this directly. However, you can use Linq to do this:
List<int> termsLst=new List<int>();
for (int runs = 0; runs < 400; runs++)
{
termsLst.Add(runs);
}
int[] terms = termsLst.ToArray();
If the array terms wasn't empty in the beginning, you can convert it to List first then do your stuf. Like:
List<int> termsLst = terms.ToList();
for (int runs = 0; runs < 400; runs++)
{
termsLst.Add(runs);
}
terms = termsLst.ToArray();
Note: don't miss adding 'using System.Linq;' at the begaining of the file.
This seems like a lot less trouble to me:
var usageList = usageArray.ToList();
usageList.Add("newstuff");
usageArray = usageList.ToArray();
Just a different approach:
int runs = 0;
bool batting = true;
string scorecard;
while (batting = runs < 400)
scorecard += "!" + runs++;
return scorecard.Split("!");
int[] terms = new int[400];
for(int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
static void Main(string[] args)
{
int[] arrayname = new int[5];/*arrayname is an array of 5 integer [5] mean in array [0],[1],[2],[3],[4],[5] because array starts with zero*/
int i, j;
/*initialize elements of array arrayname*/
for (i = 0; i < 5; i++)
{
arrayname[i] = i + 100;
}
/*output each array element value*/
for (j = 0; j < 5; j++)
{
Console.WriteLine("Element and output value [{0}]={1}",j,arrayname[j]);
}
Console.ReadKey();/*Obtains the next character or function key pressed by the user.
The pressed key is displayed in the console window.*/
}
/*arrayname is an array of 5 integer*/
int[] arrayname = new int[5];
int i, j;
/*initialize elements of array arrayname*/
for (i = 0; i < 5; i++)
{
arrayname[i] = i + 100;
}
To add the list values to string array using C# without using ToArray() method
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("three");
list.Add("four");
list.Add("five");
string[] values = new string[list.Count];//assigning the count for array
for(int i=0;i<list.Count;i++)
{
values[i] = list[i].ToString();
}
Output of the value array contains:
one
two
three
four
five
You can do this is with a list. here is how
List<string> info = new List<string>();
info.Add("finally worked");
and if you need to return this array do
return info.ToArray();
Here is one way how to deal with adding new numbers and strings to Array:
int[] ids = new int[10];
ids[0] = 1;
string[] names = new string[10];
do
{
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine("Enter Name");
names[i] = Convert.ToString(Console.ReadLine());
Console.WriteLine($"The Name is: {names[i]}");
Console.WriteLine($"the index of name is: {i}");
Console.WriteLine("Enter ID");
ids[i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"The number is: {ids[i]}");
Console.WriteLine($"the index is: {i}");
}
} while (names.Length <= 10);

Categories

Resources