hi
im new to c# and am looking for a way to radomly shuffle an array and split it into 2 groups.
i cant find a way to do this so far :(
any steps in the right direction would be gratefully recieved
many thanks
z
hi
im new to c# and am looking for a way to radomly shuffle an array and split it into 2 groups.
i cant find a way to do this so far :(
any steps in the right direction would be gratefully recieved
many thanks
z
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Shuffle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// create variable to hold a random number
int num;
// create a string array
string[] words = new string[8];
words[0] = "this";
words[1] = "is";
words[2] = "how";
words[3] = "you";
words[4] = "shuffle";
words[5] = "a";
words[6] = "array";
words[7] = "string";
Random rnd = new Random();
// loop
for (int i = 0; i < words.Length; i++)
{
// create random number
num = rnd.Next(8);
// display random string in label1
label1.Text += words[num] + "\n";
// create new random number
num = rnd.Next(8);
// display new random string in label2
label2.Text += words[num] + "\n";
}
}
}
}
Can't get much more simple that that. Hope it helps.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Shuffle { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // create variable to hold a random number int num; // create a string array string[] words = new string[8]; words[0] = "this"; words[1] = "is"; words[2] = "how"; words[3] = "you"; words[4] = "shuffle"; words[5] = "a"; words[6] = "array"; words[7] = "string"; Random rnd = new Random(); // loop for (int i = 0; i < words.Length; i++) { // create random number num = rnd.Next(8); // display random string in label1 label1.Text += words[num] + "\n"; // create new random number num = rnd.Next(8); // display new random string in label2 label2.Text += words[num] + "\n"; } } } }
Can't get much more simple that that. Hope it helps.
thanks for your quick response, il give it a whirl and see if i can modify ot for what i want. il post the results if i can :) thanks
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Random random = new Random();
var sortedList = list.OrderBy(i => random.Next()).ToList();
foreach (int i in sortedList)
Console.WriteLine(i);
// split list into 2
var group1 = sortedList.Take(5);
var group2 = sortedList.Skip(5).Take(5);
Console.Read();
You could use generics and extensions for this:
public static void Shuffle<T>(this IList<T> list)
{
Random rng = new Random();
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;
}
}
Calling it:
private void button1_Click(object sender, EventArgs e)
{
List<string> lst = new List<string>();
lst.Add("a");
lst.Add("b");
lst.Add("c");
lst.Add("d");
lst.Shuffle();
lst.ForEach(new Action<string>(Console.WriteLine));
}
i been thinking and playing with my layout and it seems to be getting more complicated by the day!
as it stands now i have 10 textBox's for the user to enter in names of players he wants to shuffle.
i assume the best way to do this is to add all the textBox inputs into an array ? ( note , names must be able to have numbers in aswell )
eg textBox1.text = myArray[0] ?
like i said before i want to then shuffle and split the array into 2 seperate teams, and post the output into another part of the form, so the result looks like this
1.................................................5
2.................................................6
3.................................................7
4 ................................................8
5.................................................9.
i dont know if u can output this into a muli line label or something, or some sort of table ? this is all supposed to happen on the click of my Shuffle button.
if anyone can comment on how to do this or a if there is a guide out there that may be relevant that would be great. if not im left with seing whqat ppl come up with on here and trying to disect it myself!
thanks so far for all the input and help. everything is appriciated gratefully.
You say array, but I prefer a generic collection. Here is some code that accomplishes the stated goal and uses part of sknake's suggestion by making the shuffler generic, as well.
private void button1_Click(object sender, EventArgs e)
{
List<TextBox> boxes = new List<TextBox>()
{ textBox1, textBox2, textBox3,
textBox4, textBox5, textBox6,
textBox7, textBox8, textBox9,
textBox10 };
List<string> names = new List<string>();
foreach (TextBox box in boxes)
names.Add(box.Text);
Random random = new Random();
var shuffledNames = this.Shuffle(names);
for (int i = 0; i < boxes.Count; i++)
boxes[i].Text = shuffledNames[i];
}
private List<T> Shuffle<T>(IList<T> list)
{
Random random = new Random();
var shuffledList = list.OrderBy(item => random.Next());
return shuffledList.ToList();
}
As mentioned above, the List<T> is easier to use; it dynamically resizes and offers more functionality.
Ive seen a number of discussions about which is quicker but never seen a difinitive answer.
I am also trying to generate random number into an array BETWEEN 0-8. I tried using
Random ranNum = new Random();
But it generates duplicates.
I also tried to shuffle the indexOf the array but it also generated duplicates.
Could any one please help me get this working?
here is my code so far.
int[] myArray = new int[9];
Random ranNum = new Random();
for (int i = 1; i < 9; i++)
{
myArray[0] = 0;
myArray[i] = ranNum.Next(1,9);
}
Console.WriteLine("+===+===+===+");
Console.WriteLine("+ " + myArray[0] + " + " + myArray[1] + " + " + myArray[2] + " +");
Console.WriteLine("+ " + myArray[3] + " + " + myArray[4] + " + " + myArray[5] + " +");
Console.WriteLine("+ " + myArray[6] + " + " + myArray[7] + " + " + myArray[8] + " +");
Console.WriteLine("+===+===+===+");
THANKZ :)
Firstly, Random is exactly that, random. Each call to Random.Next() will produce a random value, it doesnt take into account the value already produced.
Secondly, please refrain from piggybacking on old threads. If you are trying to sort your array randomly the code provided here is complete and working. If you have a different issue, please create a new thread.
As I read this post, I found it to be very helpful. Thank you for posting it. I enjoyed reading it.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.