I am creating a random number generator in c#
I generate the numbers as so
Random RandomClass = new Random();
Num1.text = RandomClass.Next(1,49).ToString();
Num2.text = RandomClass.Next(1,49).ToString();
Num3.text = RandomClass.Next(1,49).ToString();
Num4.text = RandomClass.Next(1,49).ToString();
Num5.text = RandomClass.Next(1,49).ToString();
Num6.text = RandomClass.Next(1,49).ToString();
The user clicks a button and the numbers are generated, what I want is for there to be a button which can sort the numbers, so for example smallest to lowest.
Could I turn the numbers generated into an array and call .ToArray and then sort from there? I am unsure how to group the random numbers together to then call a sorting method on them.
Just add the random numbers to a list and sort them.
Random RandomClass = new Random();
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
list.Add(RandomClass.Next(1, 49));
list.Sort();
// If you need to reverse it...
list.Reverse();
If you are comparing to a List<string> you will need to write a comparer.
Example:
private static int CompareValues(string x, string y)
{
if (x == null)
return y == null ? 0 : -1;
else
{
if (y == null)
return 1;
else
{
int left = Int32.Parse(x);
int right = Int32.Parse(y);
if (left > right)
return 1;
else if (left < right)
return -1;
else
return 0;
}
}
}
Then to use it, pass it as argument to the Sort() method. This will now be used as a custom compare to handle the integer values and sort them properly.
list.Sort(CompareValues);
If you store the random numbers in a list, you could sort (or manipulate them in a number of ways) easily using Linq. Assuming your method returns random integers:
var myRandomNumbers = new List<int>
{
RandomClass.Next(1,49),
RandomClass.Next(1,49)
};
var mySortedRandomNumbers = myRandomNumbers.OrderByDescending(x => x).ToList();
Of course, if you're always calling RandomClass.Next(1,49) you could optimize with a loop and use .Add() on the list.
Here's a solution that generates random numbers and saves them for later.
//fields
private readonly Random _random = new Random();
private readonly List<int> _randomNumbers = new List<int>();
public void SomeEvent(object sender, EventArgs e)
{
_randomNumbers.Clear();
Num1.text = GetRandom().First().ToString();
Num2.text = GetRandom().First().ToString();
Num3.text = GetRandom().First().ToString();
Num4.text = GetRandom().First().ToString();
Num5.text = GetRandom().First().ToString();
Num6.text = GetRandom().First().ToString();
}
public void SortRandomNumbers(object sender, EventArgs e)
{
var sortedRandoms = _randomNumbers;
sortedRandoms.Sort();
//do stuff with sortedRandoms
}
// Gets a random value and caches it
private IEnumerable<int> GetRandom()
{
while(true)
{
var value = _random.Next(1, 49);
_randomNumbers.Add(value);
yield return _randomNumbers.Last();
}
}
If you always have six fields Num1 - Num6, you could add their content to a List<int>, sort them by using .OrderBy() and fill them up again using the ordered list.
List<int> nums = new List<int>();
nums.Add(Convert.ToInt32(Num1.text));
.
.
nums.Add(Convert.ToInt32(Num6.text));
nums = nums.OrderBy(num => num).ToList(); //or OrderByDescending
Num1.text = nums[0];
.
.
Num6.text = nums[5];
Yes, you could use an Array:
RandomClass.Next(1,49).ToString();
ArrayList numbers = new ArrayList();
for (int i = 0; i < 10; i++)
numbers[i] = RandomClass.Next(1,49).ToString();
numbers.Sort();
Related
Please check the code below. I am trying to set value to a random property of a int list. Problem is that even after i set 5 to a random list this value getting inserted to that property. What am I doing wrong here?
var TransactionList = new List<int>();
for (int i = 0; i < 59; i++)
{
TransactionList.Add(0);
}
var randTransaction = TransactionList.OrderBy(x => Guid.NewGuid()).FirstOrDefault();
//here i am trying to set 5 value to a random TrnasactionList but this not being set
randTransaction = 5;
Try like below. new Random().Next(0, 59); will return value between 0 and 59. Or you can better set it like new Random().Next(0, TransactionList.Count); for it to be dynamic with list.
new Random().Next(minValue, maxValue); The maxValue for the upper-bound in the Next() method is exclusive—the range includes minValue, maxValue-1, and all numbers in between.
var TransactionList = new List<int>();
for (int i = 0; i < 59; i++)
{
TransactionList.Add(0);
}
// var index = new Random().Next(0, 59);
// Below will work for dynamic length of list.
var index = new Random().Next(0, TransactionList.Count);
TransactionList[index] = 5;
If you don't mind the original list getting sorted you could do this:
class Program
{
static void Main(string[] args)
{
var transactionList = new List<int>();
for (int i = 0; i < 59; i++)
{
//I initialized the list with i instead of 0 to better see sorting in place
transactionList.Add(i);
}
transactionList.Sort(new RandomComparer());
//changed it to 99 to spot it more easily
transactionList[0] = 99;
foreach (var i in transactionList)
Console.WriteLine(i);
}
}
public class RandomComparer : IComparer<int>
{
private Random _random = new Random();
public int Compare(int x, int y)
{
return _random.Next(-1, 2);
}
}
See it in action:
https://dotnetfiddle.net/NKuPdx
randTransaction is "int" data type, which is primitive data type.
if you want to set randTransaction that reflect to it's object, just set the object it self
I am a novice in Xamarin ,
I want to generate randomly 4 numbers which are in a list and this 4 numbers must be different .
In the example below I have a list of Ids and I am trying to pick 4 id randomly in the list and those 4 Ids must be each differents.
Here is my methode, I cannot see how I can continue and make it simple :
public MyWordsList()
{
InitializeComponent();
Dictionary<int, int> WordId = new Dictionary<int, int>();
int u= 0;
// TestAnswer.IsVisible = false;
foreach (var w in mywords)
{
WordId[u] = w.ID;
u++;
}
Random rnd = new Random();
// this is not ok because I can have the same number
word11.Text = WordsList[rnd.Next(1, 20)];
word12.Text = WordsList[rnd.Next(1, 20)];
word13.Text = WordsList[rnd.Next(1, 20)];
word14.Text = WordsList[rnd.Next(1, 20)];
}
If you have a better solution, I will take.
Thanks
You can write a short generic function which picks X amount of random and unique items from a specified collection and returns them:
private static IEnumerable<T> GetUniqueRandomItems<T>(int count, IList<T> allItems)
{
if (new HashSet<T>(allItems).Count < count)
{
throw new ArgumentException(nameof(allItems));
}
Random random = new Random();
HashSet<T> items = new HashSet<T>();
while (items.Count < count)
{
T value = allItems[random.Next(0, allItems.Count)];
items.Add(value);
}
return items;
}
You can later use it like this:
string[] randomIds = GetUniqueRandomItems(4, WordsList).ToArray();
word11.Text = randomIds[0];
word12.Text = randomIds[1];
word13.Text = randomIds[2];
word14.Text = randomIds[3];
call a method like the following:
private int CreateUniqueRandom(int min, int max, ICollection<int> existingNums)
{
var rnd = new Random();
var newNum = rnd.Next(min, max);
while (existingNums.Contains(newNum))
newNum = rnd.Next(min, max);
return newNum;
}
Passing through a list of the numbers that you have created so far
You probably won't need this, but just to show a method of unique random number generation with no duplicate check:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var randoms = GenerateRandoms(10, 1, 10).OrderBy(v => v);
foreach (var random in randoms)
{
Console.WriteLine(random);
}
Console.ReadLine();
}
private static int[] GenerateRandoms(int randomCount, int min, int max)
{
var length = max - min + 1;
if (randomCount > length) { throw new ArgumentException($"Cannot generate {randomCount} random numbers between {min} and {max} (inclusive)."); }
var values = new List<int>(length);
for (var i = 0; i < length; i++)
{
values.Insert(i, min + i);
}
var randomGenerator = new Random();
var randoms = new List<int>();
for (var i = 0; i < randomCount; i++)
{
var val = randomGenerator.Next(0, values.Count);
randoms.Add(values[val]);
values.RemoveAt(val);
}
return randoms.ToArray();
}
}
}
This question already has answers here:
C# creating a list of random unique integers
(6 answers)
Closed 4 years ago.
How I can put randomize numbers to list without repetition?
Here's my code, sometimes the numbers are repeated but I do not know why
Random losowa = new Random();
List<int> pula = new List<int>();
private void LosujPytania()
{
int a = losowa.Next(1,20);
while (pula.Count < 10)
{
foreach (int i in pula)
{
if (a == i)
{
a = losowa.Next(1, 20);
break;
}
}
pula.Add(a);
}
}
Code below create list of numbers without repetition. Key to resolve problem is use list.Contains().
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var list = new List<int>();
var rand = new Random();
while(list.Count <10)
{
var number = rand.Next(1,20);
if(! list.Contains(number))
list.Add(number);
}
foreach(var item in list)
Console.WriteLine(item);
}
}
You should check if this number was previously generated and generate another number if it is repeated.
Change this:
private void LosujPytania()
{
int a = losowa.Next(1,20);
while (pula.Count < 10)
{
foreach (int i in pula)
{
if (a == i)
{
a = losowa.Next(1, 20);
break;
}
}
pula.Add(a);
}
}
by this:
private void LosujPytania()
{
int a = losowa.Next(1,20);
pula.Add(a);
while (pula.Count < 10)
{
do
{
a = losowa.Next(1, 20);
} while(pula.Contains(a));
pula.Add(a);
}
}
You can do this, you can check my comments additional details:
private static void LosujPytania()
{
Random losowa = new Random();
List<int> pula = new List<int>();
int a = losowa.Next(1,20);
while (pula.Count < 10)
{
//Your code is not really checking for duplicates so I replace it with boolean condition below
//foreach (int i in pula)
//{
// if (a == i)
// {
// a = losowa.Next(1, 20);
// break;
// }
//}
a = losowa.Next(1, 20);
//This will check if your list doesn't contain your numbers yet before adding to make sure everything is unique
if (!pula.Contains(a))
pula.Add(a);
}
}
if you want numbers without replications,
I think it will be better to use HashSet.
Random losowa = new Random();
HashSet<int> pula = new HashSet<int>();
while (pula.Count < 10)
{
pula.Add(r.Next(20));
}
or if you really need the List, you can use a helper method
something like:
private void LosujPytania()
{
Random losowa = new Random();
List<int> pula = new List<int>();
int a = losowa.Next(1, 20);
pula.AddRange(Get10RandomNumbers(losowa));
}
private IEnumerable<int> Get10RandomNumbers(Random losowa)
{
HashSet<int> ints = new HashSet<int>();
while (ints.Count < 10)
{
ints.Add(losowa.Next(20));
}
return ints;
}
A simple 2 lines solution:
var rnd = new Random();
var list = Enumerable.Range(0, 20).OrderBy(x => rnd.Next()).Take(10).ToList();
Explanation:
Enumerable.Range(0, 20) will return an IEnumerable<int> with the numbers 0 to 19.
OrderBy(x => rnd.Next()) will sort the values to a random order.
Take(10) will return the first 10 numbers from the IEnumerable<int>,
and finally, ToList() will return a list of these int values.
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.
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);
}