Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Randomize from array

Randomize from array

Scheduled Pinned Locked Moved C#
data-structureshelpquestionlounge
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Socheat Net
    wrote on last edited by
    #1

    Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks

    Socheat

    OriginalGriffO L S Y 4 Replies Last reply
    0
    • S Socheat Net

      Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks

      Socheat

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      All you are doing there is clogging up your processor, to no good end. Do it this way: Create a class level Timer, call it randomTimer. Move your Random to class level. In your Start_Click event handler, check if there is an existing Timer in randomTimer. If not, create an instance, set the interval to 1/10th second, and set a handler for the Tick event. In all cases, start the timer. In the Tick event, set the lblNumber.Text to your random value. In your Stop_Click event handler, stop the timer.

      Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      S 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        All you are doing there is clogging up your processor, to no good end. Do it this way: Create a class level Timer, call it randomTimer. Move your Random to class level. In your Start_Click event handler, check if there is an existing Timer in randomTimer. If not, create an instance, set the interval to 1/10th second, and set a handler for the Tick event. In all cases, start the timer. In the Tick event, set the lblNumber.Text to your random value. In your Stop_Click event handler, stop the timer.

        Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

        S Offline
        S Offline
        Socheat Net
        wrote on last edited by
        #3

        Hi, Can you write me example? Thanks

        Socheat

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          All you are doing there is clogging up your processor, to no good end. Do it this way: Create a class level Timer, call it randomTimer. Move your Random to class level. In your Start_Click event handler, check if there is an existing Timer in randomTimer. If not, create an instance, set the interval to 1/10th second, and set a handler for the Tick event. In all cases, start the timer. In the Tick event, set the lblNumber.Text to your random value. In your Stop_Click event handler, stop the timer.

          Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

          S Offline
          S Offline
          Socheat Net
          wrote on last edited by
          #4

          Here code I just used timer control but still can't use bool run = true; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = true; this.Text = System.DateTime.Now.ToString(); run = true; Random rnd = new Random(); int i = 0; while (run == true) { Application.DoEvents(); i = rnd.Next(0, 1000); textBox1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { timer1_Tick(sender, e); } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; run = false; MessageBox.Show(textBox1.Text); }

          Socheat

          OriginalGriffO 2 Replies Last reply
          0
          • S Socheat Net

            Here code I just used timer control but still can't use bool run = true; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = true; this.Text = System.DateTime.Now.ToString(); run = true; Random rnd = new Random(); int i = 0; while (run == true) { Application.DoEvents(); i = rnd.Next(0, 1000); textBox1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { timer1_Tick(sender, e); } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; run = false; MessageBox.Show(textBox1.Text); }

            Socheat

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            No. Read up on Timer.

               private Timer timer = new Timer();
            
                private void buttonStart\_Click(object sender, EventArgs e)
                    {
                    timer.Interval = 1000 / 100;      // Ten times a second
                    timer.Tick += new EventHandler(timer\_Tick);
                    timer.Start();
                    }
            
                void timer\_Tick(object sender, EventArgs e)
                    {
                    ... Do a single instance of your Random stuff.
                    }
            
               private void buttonStop\_Click(object sender, EventArgs e)
                    {
                    timer.Stop();
                    }
            

            Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • S Socheat Net

              Here code I just used timer control but still can't use bool run = true; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = true; this.Text = System.DateTime.Now.ToString(); run = true; Random rnd = new Random(); int i = 0; while (run == true) { Application.DoEvents(); i = rnd.Next(0, 1000); textBox1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { timer1_Tick(sender, e); } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; run = false; MessageBox.Show(textBox1.Text); }

              Socheat

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              BTW: You don't need to send me an email as well as replying - the reply automatically sends an email. Just like this one, in fact...

              Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • S Socheat Net

                Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks

                Socheat

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, your code contains a couple of mistakes against general rules, including: 1. event handlers (such as a Click handler) should execute and be done with in less than say 20 milliseconds; anything that takes longer needs a different approach, maybe a timer (a System.Windows.Forms.Timer is best for GUI related stuff), maybe a separate thread; 2. don't abuse Application.DoEvents(), there are a few situations where its use would be OK, yours isn't one of them. 3. avoid "busy loops", such as your while(run) construct, whenever there is an elegant alternative. Obeying (1) often makes (2) moot. Griff's approach is fine. PS: please use PRE tags when including code snippets. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, your code contains a couple of mistakes against general rules, including: 1. event handlers (such as a Click handler) should execute and be done with in less than say 20 milliseconds; anything that takes longer needs a different approach, maybe a timer (a System.Windows.Forms.Timer is best for GUI related stuff), maybe a separate thread; 2. don't abuse Application.DoEvents(), there are a few situations where its use would be OK, yours isn't one of them. 3. avoid "busy loops", such as your while(run) construct, whenever there is an elegant alternative. Obeying (1) often makes (2) moot. Griff's approach is fine. PS: please use PRE tags when including code snippets. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Hey Luc, what situations are there where Application.DoEvents() would be OK?

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Hey Luc, what situations are there where Application.DoEvents() would be OK?

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    I allow myself to use DoEvents() inside an event handler when I am sure there is no risk of the event handler being re-entered by doing so, and I want the GUI to settle before I continue. Case in point would be a MenuItem.Click handler on say an "Open File..." menu item, where I want the menu to disappear and the GUI restored ASAP before opening the dialog that was ordered. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    L 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      I allow myself to use DoEvents() inside an event handler when I am sure there is no risk of the event handler being re-entered by doing so, and I want the GUI to settle before I continue. Case in point would be a MenuItem.Click handler on say an "Open File..." menu item, where I want the menu to disappear and the GUI restored ASAP before opening the dialog that was ordered. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Ok, thanks

                      1 Reply Last reply
                      0
                      • S Socheat Net

                        Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks

                        Socheat

                        S Offline
                        S Offline
                        Socheat Net
                        wrote on last edited by
                        #11

                        Hi all, I can do it now. I no need to remove Application.DoEvent(); otherwise my application will hang. I just remove my while loop and add it in timer class, it OK now. Thanks.

                        Socheat

                        OriginalGriffO 1 Reply Last reply
                        0
                        • S Socheat Net

                          Hi all, I can do it now. I no need to remove Application.DoEvent(); otherwise my application will hang. I just remove my while loop and add it in timer class, it OK now. Thanks.

                          Socheat

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #12

                          If you have removed your while loop, and put the code into the timer, then you do not need Application.DoEvents(). If your app hangs without it, then you are still doing something very wrong!

                          Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          • S Socheat Net

                            Hi all, I got small trobule with randomize her my code for starting randomize private void lblStart_Click(object sender, EventArgs e) { bool run; run = true; Random d = new Random(); Application.DoEvents(); while (run == true) { Application.DoEvents(); int rnd = d.Next(0, lst.Count - 1); lblNumber.Text = lst[rnd].ToString(); } } here for stop randomize and show result in messagebox and Label private void lblStop_Click(object sender, EventArgs e) { run = false; MessageBox.Show(lblNumber.Text); } after I click stop button in label and messagebox it show the same text, but after I clcik OK button in label show another number? Can anyone help to correct this? Thanks

                            Socheat

                            Y Offline
                            Y Offline
                            yu jian
                            wrote on last edited by
                            #13
                            Random d = new Random((int)(DateTime.Now().Interval));
                            Application.DoEvents();
                            while (run == true)
                            {
                            Application.DoEvents();
                            int rnd = d.Next(0, lst.Count - 1);
                            lblNumber.Text = lst[rnd].ToString();
                            }
                            
                            }
                            
                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups