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. Need the code for Randomization of numbers using c#

Need the code for Randomization of numbers using c#

Scheduled Pinned Locked Moved C#
csharplounge
24 Posts 9 Posters 1 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.
  • V vasavi p

    I have used the following code. when the "Quescnt" and the "max value(Int32.Parse(ds.Tables[1].Rows[cnt-1][0].ToString()))" are equal, its going to infinite loop. static int TheSeed = (int)DateTime.Now.Ticks; Random RandomClass = new Random(TheSeed); string Randomnos = string.Empty; for (int i = 0; i < Quescnt ; i++) { int RandomNumber = RandomClass.Next(Int32.Parse(ds.Tables[1].Rows[0][0].ToString()), Int32.Parse(ds.Tables[1].Rows[cnt-1][0].ToString())); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } }

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

    Firstly, use the "code block" button to preserve your formatting when posting code fragments - it makes it easier to read. I have pared your fragment down to the relevent bits:

    string Randomnos = string.Empty;
    for (int i = 0; i < Quescnt ; i++)
    {
    int RandomNumber = RandomClass.Next();
    if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ","))
    {
    Randomnos = "Not an empty string any more.";
    }
    else
    {
    Quescnt++;
    }
    }

    So, when you first enter the loop, and Randomnos == "", what is going to happen?

    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

    "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

    C V 2 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      Firstly, use the "code block" button to preserve your formatting when posting code fragments - it makes it easier to read. I have pared your fragment down to the relevent bits:

      string Randomnos = string.Empty;
      for (int i = 0; i < Quescnt ; i++)
      {
      int RandomNumber = RandomClass.Next();
      if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ","))
      {
      Randomnos = "Not an empty string any more.";
      }
      else
      {
      Quescnt++;
      }
      }

      So, when you first enter the loop, and Randomnos == "", what is going to happen?

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #4

      Well spotted. I found the whole thing far too hard to read, and I always like in the first instance to try to get these people to at least try to use the debugger before asking us to do their job for them. I am losing all faith in development as a respectable profession. I think I should become a plumber.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      OriginalGriffO S H 3 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Firstly, use the "code block" button to preserve your formatting when posting code fragments - it makes it easier to read. I have pared your fragment down to the relevent bits:

        string Randomnos = string.Empty;
        for (int i = 0; i < Quescnt ; i++)
        {
        int RandomNumber = RandomClass.Next();
        if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ","))
        {
        Randomnos = "Not an empty string any more.";
        }
        else
        {
        Quescnt++;
        }
        }

        So, when you first enter the loop, and Randomnos == "", what is going to happen?

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        V Offline
        V Offline
        vasavi p
        wrote on last edited by
        #5

        hi thanks for the reply. i want to copy all the random's numbers in a string separating by a comma. i.e Randomnos = "2,5,3,1,4"; This "Quescnt" comes from database. when this Quescnt and the MaxValue in RandomClass.Next(Min, Max) are equal, its going to infinite loop.

        C OriginalGriffO 2 Replies Last reply
        0
        • V vasavi p

          hi thanks for the reply. i want to copy all the random's numbers in a string separating by a comma. i.e Randomnos = "2,5,3,1,4"; This "Quescnt" comes from database. when this Quescnt and the MaxValue in RandomClass.Next(Min, Max) are equal, its going to infinite loop.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #6

          And why is that ? Have you used the debugger to see what is going on ?

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          1 Reply Last reply
          0
          • C Christian Graus

            Well spotted. I found the whole thing far too hard to read, and I always like in the first instance to try to get these people to at least try to use the debugger before asking us to do their job for them. I am losing all faith in development as a respectable profession. I think I should become a plumber.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

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

            Whenever I see a loop termination variable that changes in the loop, I just think: "where do they get these people?"

            Christian Graus wrote:

            I think I should become a plumber

            Not a bad choice: in the UK at least, you can earn more for less work!

            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

            "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

            C 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Whenever I see a loop termination variable that changes in the loop, I just think: "where do they get these people?"

              Christian Graus wrote:

              I think I should become a plumber

              Not a bad choice: in the UK at least, you can earn more for less work!

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #8

              Yes, I agree.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              1 Reply Last reply
              0
              • V vasavi p

                hi thanks for the reply. i want to copy all the random's numbers in a string separating by a comma. i.e Randomnos = "2,5,3,1,4"; This "Quescnt" comes from database. when this Quescnt and the MaxValue in RandomClass.Next(Min, Max) are equal, its going to infinite loop.

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

                vasavi.p wrote:

                when this Quescnt and the MaxValue in RandomClass.Next(Min, Max) are equal, its going to infinite loop

                No, it's going to infinite loop under all circumstances. Try running it though the debugger. What is your "code" actually doing?

                No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                "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

                C V 2 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  vasavi.p wrote:

                  when this Quescnt and the MaxValue in RandomClass.Next(Min, Max) are equal, its going to infinite loop

                  No, it's going to infinite loop under all circumstances. Try running it though the debugger. What is your "code" actually doing?

                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #10

                  There's a debugger ?

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • C Christian Graus

                    There's a debugger ?

                    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

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

                    Yeah - you just have to enable it: Type "Format c: /s" at a command prompt and press ENTER. Just say YES to any of those silly questions, you don't need to read them...

                    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                    "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

                    N 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      vasavi.p wrote:

                      when this Quescnt and the MaxValue in RandomClass.Next(Min, Max) are equal, its going to infinite loop

                      No, it's going to infinite loop under all circumstances. Try running it though the debugger. What is your "code" actually doing?

                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                      V Offline
                      V Offline
                      vasavi p
                      wrote on last edited by
                      #12

                      hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }

                      OriginalGriffO L J D 4 Replies Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Yeah - you just have to enable it: Type "Format c: /s" at a command prompt and press ENTER. Just say YES to any of those silly questions, you don't need to read them...

                        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                        N Offline
                        N Offline
                        nagendrathecoder
                        wrote on last edited by
                        #13

                        I'll stop pressing ENTER before using this debugger. ;P

                        1 Reply Last reply
                        0
                        • V vasavi p

                          hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }

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

                          I am not even going to look at them until you have preserved the formatting - why should I even slightly struggle? Remember the "code block" button above?

                          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                          "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
                          • V vasavi p

                            hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }

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

                            I will refrain form making any comment except to point out that you have the following for loop:

                            for (int i = 0; i < Quescnt; i++)

                            and within this loop you have the following statement

                            Quescnt++;

                            Using the debugger could have helped you locate this fairly quickly.

                            J 1 Reply Last reply
                            0
                            • V vasavi p

                              hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }

                              J Offline
                              J Offline
                              J4amieC
                              wrote on last edited by
                              #16

                              vasavi.p wrote:

                              and try with this too[this will go to infinite loop]

                              Now for extra credit (that is, beyond being able to blindly paste your code into this window) can you tell us why this goes into an infinite loop? Hint:- Documentation :Random.Next(Int32,Int32)[^] Relevant part of documentation:

                              minValue
                                  Type: System..::.Int32
                                  The inclusive lower bound of the random number returned.
                              
                              maxValue
                                  Type: System..::.Int32
                                  The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
                              
                              1 Reply Last reply
                              0
                              • L Lost User

                                I will refrain form making any comment except to point out that you have the following for loop:

                                for (int i = 0; i < Quescnt; i++)

                                and within this loop you have the following statement

                                Quescnt++;

                                Using the debugger could have helped you locate this fairly quickly.

                                J Offline
                                J Offline
                                J4amieC
                                wrote on last edited by
                                #17

                                Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.

                                L 1 Reply Last reply
                                0
                                • V vasavi p

                                  hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }

                                  D Offline
                                  D Offline
                                  DeepakMega
                                  wrote on last edited by
                                  #18

                                  Read this first How to post questions[^]

                                  1 Reply Last reply
                                  0
                                  • J J4amieC

                                    Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.

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

                                    J4amieC wrote:

                                    Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.

                                    I thought s/he's complaining that it goes into an infinite loop. There seems little point in worrying about anything else until that logic is fixed.

                                    J 1 Reply Last reply
                                    0
                                    • L Lost User

                                      J4amieC wrote:

                                      Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.

                                      I thought s/he's complaining that it goes into an infinite loop. There seems little point in worrying about anything else until that logic is fixed.

                                      J Offline
                                      J Offline
                                      J4amieC
                                      wrote on last edited by
                                      #20

                                      I thought it was going into an infinite loop because its looking for the next unique random number, and all possibilities have already been found. :confused:

                                      L 1 Reply Last reply
                                      0
                                      • C Christian Graus

                                        Well spotted. I found the whole thing far too hard to read, and I always like in the first instance to try to get these people to at least try to use the debugger before asking us to do their job for them. I am losing all faith in development as a respectable profession. I think I should become a plumber.

                                        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                                        S Offline
                                        S Offline
                                        Skymir
                                        wrote on last edited by
                                        #21

                                        Christian Graus wrote:

                                        I think I should become a plumber.

                                        I've been thinking electrician. Plumbing was an idea, but you end up having to deal with septic tanks and pipes clogged with things no one ever wants to know about.

                                        The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

                                        1 Reply Last reply
                                        0
                                        • C Christian Graus

                                          Well spotted. I found the whole thing far too hard to read, and I always like in the first instance to try to get these people to at least try to use the debugger before asking us to do their job for them. I am losing all faith in development as a respectable profession. I think I should become a plumber.

                                          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                                          H Offline
                                          H Offline
                                          Henry Minute
                                          wrote on last edited by
                                          #22

                                          I wonder if you can help me? I've been trying to fit a new washer to my kitchen tap for some time. Should I use C# or VB.Net for this?

                                          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                          OriginalGriffO 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