Results 1 to 6 of 6

Thread: Randomise a name

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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;
    }
    }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Lively Member
    Join Date
    May 2008
    Location
    Manila, Philippines
    Posts
    81

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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    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!

  5. #5
    Lively Member
    Join Date
    May 2008
    Location
    Manila, Philippines
    Posts
    81

    Cool 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 ?

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Randomise a name

    This works for me

    csharp Code:
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.ComponentModel;
    5. using System.Data;
    6. using System.Drawing;
    7. using System.Text;
    8. using System.Windows.Forms;
    9.  
    10. namespace WindowsApplication4
    11. {
    12.     public partial class Form1 : Form
    13.     {
    14.         public Form1()
    15.         {
    16.             InitializeComponent();
    17.         }
    18.  
    19.         ArrayList ar = new ArrayList();
    20.  
    21.         private void Form1_Load(object sender, EventArgs e)
    22.         {
    23.             ar.Add("dee-u1");
    24.             ar.Add("dee-u2");
    25.             ar.Add("dee-u3");
    26.             ar.Add("dee-u4");
    27.             ar.Add("dee-u5");
    28.         }
    29.  
    30.         public static void ShuffleArray(ArrayList source)
    31.         {
    32.             Random rnd = new Random();
    33.             for (int inx = source.Count - 1; inx > 0; --inx)
    34.             {
    35.                 //get new randon number
    36.                 int position = rnd.Next(inx);
    37.                 //temporarily store the value of source[inx] to a variable
    38.                 object temp = source[inx];
    39.                 //set the value of the current index (inx) to the value of the item found
    40.                 //on the random position generated earlier
    41.                 source[inx] = source[position];
    42.                 //put the earlier stored value to the location of the new randon generator
    43.                 //which swaps their position
    44.                 source[position] = temp;
    45.             }
    46.         }
    47.  
    48.         private void button1_Click(object sender, EventArgs e)
    49.         {
    50.             ShuffleArray(ar);
    51.             listBox1.Items.Clear();
    52.             for (int a = 0; a <= ar.Count - 1; a++)
    53.             {
    54.                 listBox1.Items.Add(ar[a].ToString());
    55.             }
    56.         }
    57.     }
    58. }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width