How to pick one random string from given strings? - c#

How can I make a program that will pick one random string from given strings like this:
int x;
x = Random.Range(0,2);
string[] Quest0 = {"You","Are","How","Hello"};
string[] Quest1 = {"Hey","Hi","Why","Yes"};
string[] Quest2 = {"Here","Answer","One","Pick"};
I would like to print out like this:
if x = 2 it would print out Quest2 and so on.
Thank you!

List<String[]> quests = new ArrayList<String[]>();
quests.add(0, new string[]{"You","Are","How","Hello"});
quests.add(1, new string[]{"Hey","Hi","Why","Yes"});
quests.add(2, new string[]{"Here","Answer","One","Pick"});
int x = new Random().nextInt((2 - 0) + 1);
System.out.println(quests.get(x).toString());

Fistly you need to declare a random variable.
Random random = new Random();
this will create a variable in which you can now get random numbers from. to get random numbers you will use random.next(x,y) or in your case random.next(0,3) because the final argument is exclusive, so if you want 0, 1 or 2, you must use (0,3).
you then need to make some conditional statments, i would use If statments, to accomplish your goal use something like this:
if (x == 2)
{
foreach (string s in Quest2)
{
Console.WriteLine(s);
}
}
Do this for each possible outcome and it will print out all of the values in your array of strings. Hope I have been helpful, thanks.
Also if you new become familiar with these links:
http://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx
http://msdn.microsoft.com/en-gb/library/aa288453%28v=vs.71%29.aspx

Related

C# - How to find the average from the numbers given by the textbox

I am pretty new on the programming field. I started learning C# and I have a problem.
I need to find the average of the given numbers from textboxes.
int wage1 = int.Parse(textBox_wage1.Text);
int wage2 = int.Parse(textBox_wage2.Text);
int wage3 = int.Parse(textBox_wage3.Text);
int wage4 = int.Parse(textBox_wage4.Text);
int wage5 = int.Parse(textBox_wage5.Text);
int wage6 = int.Parse(textBox_wage6.Text);
int wage7 = int.Parse(textBox_wage7.Text);
var average = new int[] { wage1, wage2, wage3, wage4, wage5, wage6, wage7 };
double AverageBruto = Queryable.Average(average.AsQueryable());
lbl_AverageBruto.Text = "Average: " + AverageBruto;`
This works perfectly fine if the user has filled all 7 textboxes, but if they didn't fill at least one of them, it causes an error. can someone help me figure out how I can find the average if they fill only 3 or 4 of the textboxes? Thank you.
You likely don't want to be using an array for this, because if null values are represented by zeros then the average will be off. The wage ints should probably be in an array (The textBox_wages as well but it's not necessary for this example) so you can iterate through them, check for null values, and only add those that aren't null to a list you'll take the average from. Here's one way you could approach it:
int[] wages = new int[8];
int wages[0] = int.Parse(textBox_wage1.Text);
int wages[1] = int.Parse(textBox_wage2.Text);
int wages[2] = int.Parse(textBox_wage3.Text);
int wages[3] = int.Parse(textBox_wage4.Text);
int wages[5] = int.Parse(textBox_wage5.Text);
int wages[6] = int.Parse(textBox_wage6.Text);
int wages[7] = int.Parse(textBox_wage7.Text);
var AverageBruto = new List<int>();
foreach (int x in wages) if (x != null) AverageBruto.Add(i);
AverageBruto = AverageBruto.average();
lbl_AverageBruto.Text = "Average: " + AverageBruto;
You'll have to use System.Linq for this method though.
Let's organize the TextBoxes and then query with a help of Linq:
double AverageBruto = new TextBox[] {
textBox_wage1,
textBox_wage2,
textBox_wage3,
textBox_wage4,
textBox_wage5,
textBox_wage6,
textBox_wage7,
}
.Where(box => !string.IsNullOrWhiteSpace(box.Text))
.Average(box => int.Parse(box.Text));
lbl_AverageBruto.Text = $"Average: {AverageBruto}";
Here, with a help of Where we choose only TextBoxes which have some Text

Autogenerated number with the use of date and time c#

Having trouble on how am i going to autogenerate a number with the use of date and time. For example, i need to generate this kind of number, 16-9685 with, 16 = year and 9685 be randomized and will be shown in a textbox. i tried this one but unfortunately, this doesn't work. thanks for helping
var random = new Random(System.DateTime.Now.Year);
int randomNumber = random.Next(-1000, -1);
TextBoxSESID.Text = randomNumber.ToString();
// Keep this _r as a member, not local
Random _r = new Random();
...
// Gen a random number
int rand = _r.Next(1, 10000);
// Get the "2016-" prefix
string yearPrefix = DateTime.Now.Year + "-";
// Remove the first 2 digits of the year prefix, now it is "16-"
yearPrefix = yearPrefix.Substring(2);
// Put the year prefix together with the random number into the textbox
TextBoxSESID.Text = yearPrefix + rand;
I'm going to suggest that this is a nice clean way to produce the string you require:
TextBoxSESID.Text = $"{DateTime.Now.Year % 100}-{_r.Next(1, 10000)}";
If you're not using C# 6 then this will work too:
TextBoxSESID.Text = String.Format("{0}-{1}", DateTime.Now.Year % 100, _r.Next(1, 10000));
You still need have this as a field:
private Random _r = new Random();
If you want to ensure that no two names are repeated (assuming that you never use more than 9,999 names) then do this:
File.WriteAllLines(
"16.txt",
Enumerable
.Range(1, 9999)
.Select(n => $"{DateTime.Now.Year % 100}-{n}")
.OrderBy(x => _r.Next()));
Now you have a file that has all the possible numbers in a random order.
To get a new number do this:
var lines = File.ReadAllLines("16.txt");
var first = lines.First();
File.WriteAllLines("16.txt", lines.Skip(1));
It will take the top line and re-save the file without it.
Just make sure you create files for each year as it comes along.

dont want a random number to show twice

I am trying to save random numbers in a list, the same number can not come twice.
It´s a BINGO game where this method is used to display ex B12, then the user hits enter and a new number will show. This will keep on until the user writes q.
This works, BUT the number can show up twice...
static void bingo()
{
Random rnd =new Random();
List<int> check = new List<int>();
string choice = "";
while (choice != "Q")
{
int number = rnd.Next(1, 76);
while (!check.Contains(number))
{
kontroll.Add(number);
}
if (number <=15)
{
choice = Interaction.InputBox("B" + number);
choice = choice.ToUpper();
}
else if(number <= 30)
etc.
Something like this should work (if I'm reading your question correctly)
Enumerable.Range(1,76).OrderBy(n => rnd.NextDouble())
There are a couple of ways to do this:
Keep track of what numbers have been "called" - if a number is in the list, pick a different one
Remove numbers that have been called from the original list, then pick a new one at random each time.
Sort the list of possible values by a random number and just work though the list
The easiest way to accomplish this is to use a HashSet.
var usedNumbers = new HashSet<int>();
...
int number;
do {
number = rnd.Next(1, 76);
} while (usedNumbers.Contains(number));
usedNumbers.Add(number);

Compare a string row

Let´s say that I have a string that looks like this:
string WidthStr= "0086;0086;0086;0086;0086;0086;0086;0086;0085;";
And then i´m picking up the first number:
FirstRollWidthStr = WidthPadLeft.Substring(0, 4);
The question I have is: Is it possible to compare the data i that i got from the row below (FirstRollWidthStr = WidthPadLeft.Substring(0, 4);) with the rest?
So from FirstRollWidthStr = WidthPadLeft.Substring(0, 4); i got: 0086. And there are more numbers in string WidthStr, and the last number are 0085 so it´s different from 0086 so i want to pick up the numbers who are different from the first number.
var numbers = WidthStr.Split(';').Select(double.Parse).Distinct().ToArray();
Or if you need only numbers different from the first one then (continuing previous code):
var otherNumbers = numbers.Skip(1).Except(numbers.Take(1));
Try this:
var numbers = WidthStr.Split(';');
if(numbers.Length > 0)
{
var differentNumbers = numbers.Skip(1).Where(x => x != numbers[0]);
// ...
}

Best way to randomize an array with .NET

What is the best way to randomize an array of strings with .NET? My array contains about 500 strings and I'd like to create a new Array with the same strings but in a random order.
Please include a C# example in your answer.
The following implementation uses the Fisher-Yates algorithm AKA the Knuth Shuffle. It runs in O(n) time and shuffles in place, so is better performing than the 'sort by random' technique, although it is more lines of code. See here for some comparative performance measurements. I have used System.Random, which is fine for non-cryptographic purposes.*
static class RandomExtensions
{
public static void Shuffle<T> (this Random rng, T[] array)
{
int n = array.Length;
while (n > 1)
{
int k = rng.Next(n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
}
Usage:
var array = new int[] {1, 2, 3, 4};
var rng = new Random();
rng.Shuffle(array);
rng.Shuffle(array); // different order from first call to Shuffle
* For longer arrays, in order to make the (extremely large) number of permutations equally probable it would be necessary to run a pseudo-random number generator (PRNG) through many iterations for each swap to produce enough entropy. For a 500-element array only a very small fraction of the possible 500! permutations will be possible to obtain using a PRNG. Nevertheless, the Fisher-Yates algorithm is unbiased and therefore the shuffle will be as good as the RNG you use.
If you're on .NET 3.5, you can use the following IEnumerable coolness:
Random rnd=new Random();
string[] MyRandomArray = MyArray.OrderBy(x => rnd.Next()).ToArray();
Edit: and here's the corresponding VB.NET code:
Dim rnd As New System.Random
Dim MyRandomArray = MyArray.OrderBy(Function() rnd.Next()).ToArray()
Second edit, in response to remarks that System.Random "isn't threadsafe" and "only suitable for toy apps" due to returning a time-based sequence: as used in my example, Random() is perfectly thread-safe, unless you're allowing the routine in which you randomize the array to be re-entered, in which case you'll need something like lock (MyRandomArray) anyway in order not to corrupt your data, which will protect rnd as well.
Also, it should be well-understood that System.Random as a source of entropy isn't very strong. As noted in the MSDN documentation, you should use something derived from System.Security.Cryptography.RandomNumberGenerator if you're doing anything security-related. For example:
using System.Security.Cryptography;
...
RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
string[] MyRandomArray = MyArray.OrderBy(x => GetNextInt32(rnd)).ToArray();
...
static int GetNextInt32(RNGCryptoServiceProvider rnd)
{
byte[] randomInt = new byte[4];
rnd.GetBytes(randomInt);
return Convert.ToInt32(randomInt[0]);
}
You're looking for a shuffling algorithm, right?
Okay, there are two ways to do this: the clever-but-people-always-seem-to-misunderstand-it-and-get-it-wrong-so-maybe-its-not-that-clever-after-all way, and the dumb-as-rocks-but-who-cares-because-it-works way.
Dumb way
Create a duplicate of your first array, but tag each string should with a random number.
Sort the duplicate array with respect to the random number.
This algorithm works well, but make sure that your random number generator is unlikely to tag two strings with the same number. Because of the so-called Birthday Paradox, this happens more often than you might expect. Its time complexity is O(n log n).
Clever way
I'll describe this as a recursive algorithm:
To shuffle an array of size n (indices in the range [0..n-1]):
if n = 0
do nothing
if n > 0
(recursive step) shuffle the first n-1 elements of the array
choose a random index, x, in the range [0..n-1]
swap the element at index n-1 with the element at index x
The iterative equivalent is to walk an iterator through the array, swapping with random elements as you go along, but notice that you cannot swap with an element after the one that the iterator points to. This is a very common mistake, and leads to a biased shuffle.
Time complexity is O(n).
This algorithm is simple but not efficient, O(N2). All the "order by" algorithms are typically O(N log N). It probably doesn't make a difference below hundreds of thousands of elements but it would for large lists.
var stringlist = ... // add your values to stringlist
var r = new Random();
var res = new List<string>(stringlist.Count);
while (stringlist.Count >0)
{
var i = r.Next(stringlist.Count);
res.Add(stringlist[i]);
stringlist.RemoveAt(i);
}
The reason why it's O(N2) is subtle: List.RemoveAt() is a O(N) operation unless you remove in order from the end.
You can also make an extention method out of Matt Howells. Example.
namespace System
{
public static class MSSystemExtenstions
{
private static Random rng = new Random();
public static void Shuffle<T>(this T[] array)
{
rng = new Random();
int n = array.Length;
while (n > 1)
{
int k = rng.Next(n);
n--;
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
}
}
Then you can just use it like:
string[] names = new string[] {
"Aaron Moline1",
"Aaron Moline2",
"Aaron Moline3",
"Aaron Moline4",
"Aaron Moline5",
"Aaron Moline6",
"Aaron Moline7",
"Aaron Moline8",
"Aaron Moline9",
};
names.Shuffle<string>();
Just thinking off the top of my head, you could do this:
public string[] Randomize(string[] input)
{
List<string> inputList = input.ToList();
string[] output = new string[input.Length];
Random randomizer = new Random();
int i = 0;
while (inputList.Count > 0)
{
int index = r.Next(inputList.Count);
output[i++] = inputList[index];
inputList.RemoveAt(index);
}
return (output);
}
Randomizing the array is intensive as you have to shift around a bunch of strings. Why not just randomly read from the array? In the worst case you could even create a wrapper class with a getNextString(). If you really do need to create a random array then you could do something like
for i = 0 -> i= array.length * 5
swap two strings in random places
The *5 is arbitrary.
public static void Shuffle(object[] arr)
{
Random rand = new Random();
for (int i = arr.Length - 1; i >= 1; i--)
{
int j = rand.Next(i + 1);
object tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
Generate an array of random floats or ints of the same length. Sort that array, and do corresponding swaps on your target array.
This yields a truly independent sort.
Ok, this is clearly a bump from my side (apologizes...), but I often use a quite general and cryptographically strong method.
public static class EnumerableExtensions
{
static readonly RNGCryptoServiceProvider RngCryptoServiceProvider = new RNGCryptoServiceProvider();
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> enumerable)
{
var randomIntegerBuffer = new byte[4];
Func<int> rand = () =>
{
RngCryptoServiceProvider.GetBytes(randomIntegerBuffer);
return BitConverter.ToInt32(randomIntegerBuffer, 0);
};
return from item in enumerable
let rec = new {item, rnd = rand()}
orderby rec.rnd
select rec.item;
}
}
Shuffle() is an extension on any IEnumerable so getting, say, numbers from 0 to 1000 in random order in a list can be done with
Enumerable.Range(0,1000).Shuffle().ToList()
This method also wont give any surprises when it comes to sorting, since the sort value is generated and remembered exactly once per element in the sequence.
Random r = new Random();
List<string> list = new List(originalArray);
List<string> randomStrings = new List();
while(list.Count > 0)
{
int i = r.Random(list.Count);
randomStrings.Add(list[i]);
list.RemoveAt(i);
}
Jacco, your solution ising a custom IComparer isn't safe. The Sort routines require the comparer to conform to several requirements in order to function properly. First among them is consistency. If the comparer is called on the same pair of objects, it must always return the same result. (the comparison must also be transitive).
Failure to meet these requirements can cause any number of problems in the sorting routine including the possibility of an infinite loop.
Regarding the solutions that associate a random numeric value with each entry and then sort by that value, these are lead to an inherent bias in the output because any time two entries are assigned the same numeric value, the randomness of the output will be compromised. (In a "stable" sort routine, whichever is first in the input will be first in the output. Array.Sort doesn't happen to be stable, but there is still a bias based on the partitioning done by the Quicksort algorithm).
You need to do some thinking about what level of randomness you require. If you are running a poker site where you need cryptographic levels of randomness to protect against a determined attacker you have very different requirements from someone who just wants to randomize a song playlist.
For song-list shuffling, there's no problem using a seeded PRNG (like System.Random). For a poker site, it's not even an option and you need to think about the problem a lot harder than anyone is going to do for you on stackoverflow. (using a cryptographic RNG is only the beginning, you need to ensure that your algorithm doesn't introduce a bias, that you have sufficient sources of entropy, and that you don't expose any internal state that would compromise subsequent randomness).
This post has already been pretty well answered - use a Durstenfeld implementation of the Fisher-Yates shuffle for a fast and unbiased result. There have even been some implementations posted, though I note some are actually incorrect.
I wrote a couple of posts a while back about implementing full and partial shuffles using this technique, and (this second link is where I'm hoping to add value) also a follow-up post about how to check whether your implementation is unbiased, which can be used to check any shuffle algorithm. You can see at the end of the second post the effect of a simple mistake in the random number selection can make.
You don't need complicated algorithms.
Just one simple line:
Random random = new Random();
array.ToList().Sort((x, y) => random.Next(-1, 1)).ToArray();
Note that we need to convert the Array to a List first, if you don't use List in the first place.
Also, mind that this is not efficient for very large arrays! Otherwise it's clean & simple.
This is a complete working Console solution based on the example provided in here:
class Program
{
static string[] words1 = new string[] { "brown", "jumped", "the", "fox", "quick" };
static void Main()
{
var result = Shuffle(words1);
foreach (var i in result)
{
Console.Write(i + " ");
}
Console.ReadKey();
}
static string[] Shuffle(string[] wordArray) {
Random random = new Random();
for (int i = wordArray.Length - 1; i > 0; i--)
{
int swapIndex = random.Next(i + 1);
string temp = wordArray[i];
wordArray[i] = wordArray[swapIndex];
wordArray[swapIndex] = temp;
}
return wordArray;
}
}
int[] numbers = {0,1,2,3,4,5,6,7,8,9};
List<int> numList = new List<int>();
numList.AddRange(numbers);
Console.WriteLine("Original Order");
for (int i = 0; i < numList.Count; i++)
{
Console.Write(String.Format("{0} ",numList[i]));
}
Random random = new Random();
Console.WriteLine("\n\nRandom Order");
for (int i = 0; i < numList.Capacity; i++)
{
int randomIndex = random.Next(numList.Count);
Console.Write(String.Format("{0} ", numList[randomIndex]));
numList.RemoveAt(randomIndex);
}
Console.ReadLine();
Could be:
Random random = new();
string RandomWord()
{
const string CHARS = "abcdefghijklmnoprstuvwxyz";
int n = random.Next(CHARS.Length);
return string.Join("", CHARS.OrderBy(x => random.Next()).ToArray())[0..n];
}
Here's a simple way using OLINQ:
// Input array
List<String> lst = new List<string>();
for (int i = 0; i < 500; i += 1) lst.Add(i.ToString());
// Output array
List<String> lstRandom = new List<string>();
// Randomize
Random rnd = new Random();
lstRandom.AddRange(from s in lst orderby rnd.Next(100) select s);
private ArrayList ShuffleArrayList(ArrayList source)
{
ArrayList sortedList = new ArrayList();
Random generator = new Random();
while (source.Count > 0)
{
int position = generator.Next(source.Count);
sortedList.Add(source[position]);
source.RemoveAt(position);
}
return sortedList;
}

Categories