|
-
Feb 2nd, 2009, 03:50 PM
#1
Thread Starter
Frenzied Member
Randomise a name
hey,
I am inserting a names in a textbox one by one and I would like to randomise a name in the arraylist. Can anyone help me continue this bit with randomising please
Thanks
Code:
namespace Arraylist1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string nums;
ArrayList m_text = new ArrayList();
string text_line;
private void button1_Click(object sender, EventArgs e)
{
text_line = textBox1.Text;
if( text_line != null )
{
// insert the line at the back of the container
m_text.Add( text_line );
nums = Convert.ToString(m_text.Count);
textBox1.Text = "";
}
}
private void button2_Click(object sender, EventArgs e)
{ // let's see how many we actually added ...
MessageBox.Show("We inserted" + nums + " items");
*******************
}
}
}
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Feb 2nd, 2009, 03:57 PM
#2
Re: Randomise a name
Could you try this if it will work for you?
Code:
public static void ShuffleArray(ArrayList source)
{
Random rnd = new Random();
for (int inx = source.Length-1; inx > 0; --inx)
{
int position = rnd.Next(inx);
object temp = source[inx];
source[inx] = source[position];
source[position] = temp;
}
}
-
Feb 4th, 2009, 01:29 AM
#3
Lively Member
Re: Randomise a name
try this
private void btnEnter_Click(object sender,EventArgs e) {
if(!string.IsNullOrEmpty(textName.Text.Trim())) {
listBox1.Items.Add(textName.Text);
textName.Clear();
textName.Focus();
}
}
private void btnRandomise_Click(object sender,EventArgs e) {
Random rnd = new Random();
List<string> randomised = new List<string>();
int cntr = 0;
while(cntr != listBox1.Items.Count) {
int pos = rnd.Next(1,listBox1.Items.Count + 1);
if(!listBox2.Items.Contains(listBox1.Items[pos - 1].ToString())
/*&& !randomised.Contains(listBox1.Text[pos-1].ToString())*/) {
listBox2.Items.Add(listBox1.Items[pos - 1]);
randomised.Add(listBox1.Items[pos - 1].ToString());
cntr++;
}
}
}
-
Feb 4th, 2009, 01:56 PM
#4
Thread Starter
Frenzied Member
Re: Randomise a name
hi Dee-u,
Im trying to implement your function. I have included this function in a button event but nothing is happening.
What I need to hav ethe program do is for the user to enter some names one by one that is refreshed but a button click. Then I need to randomise the names and displayed in a label. I guess this last bit of label displayed is missing however I dont know how to continue.
One more thing pls can you do some inline comments along with each line so as to follow better.
Many thanks
------------------------------------------------------------------------
If an answer to your question has been helpful, then please, Rate it! 
-
Feb 5th, 2009, 03:46 AM
#5
Lively Member
Re: Randomise a name
have you seen my code?
Code:
try this
private void btnEnter_Click(object sender,EventArgs e) {
if(!string.IsNullOrEmpty(textName.Text.Trim())) {
listBox1.Items.Add(textName.Text);
textName.Clear();
textName.Focus();
}
}
private void btnRandomise_Click(object sender,EventArgs e) {
Random rnd = new Random();
List<string> randomised = new List<string>();
int cntr = 0;
while(cntr != listBox1.Items.Count) {
int pos = rnd.Next(1,listBox1.Items.Count + 1);
if(!listBox2.Items.Contains(listBox1.Items[pos - 1].ToString())
/*&& !randomised.Contains(listBox1.Text[pos-1].ToString())*/) {
listBox2.Items.Add(listBox1.Items[pos - 1]);
randomised.Add(listBox1.Items[pos - 1].ToString());
cntr++;
}
}
}
i think this is what you need ?
-
Feb 5th, 2009, 07:51 AM
#6
Re: Randomise a name
This works for me
csharp Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ArrayList ar = new ArrayList();
private void Form1_Load(object sender, EventArgs e)
{
ar.Add("dee-u1");
ar.Add("dee-u2");
ar.Add("dee-u3");
ar.Add("dee-u4");
ar.Add("dee-u5");
}
public static void ShuffleArray(ArrayList source)
{
Random rnd = new Random();
for (int inx = source.Count - 1; inx > 0; --inx)
{
//get new randon number
int position = rnd.Next(inx);
//temporarily store the value of source[inx] to a variable
object temp = source[inx];
//set the value of the current index (inx) to the value of the item found
//on the random position generated earlier
source[inx] = source[position];
//put the earlier stored value to the location of the new randon generator
//which swaps their position
source[position] = temp;
}
}
private void button1_Click(object sender, EventArgs e)
{
ShuffleArray(ar);
listBox1.Items.Clear();
for (int a = 0; a <= ar.Count - 1; a++)
{
listBox1.Items.Add(ar[a].ToString());
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|