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. randomization of numbers without duplication in C#

randomization of numbers without duplication in C#

Scheduled Pinned Locked Moved C#
csharp
16 Posts 7 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.
  • V vasavi p

    Need the randomization of numbers without repeatation using c#

    B Offline
    B Offline
    benjymous
    wrote on last edited by
    #3

    here's some pseudocode

    repeat
    r = get random number
    until r is a new unique number
    remember you've seen r before

    Implementation is left as an exercise to the reader

    Help me! I'm turning into a grapefruit! Buzzwords!

    L 1 Reply Last reply
    0
    • B benjymous

      here's some pseudocode

      repeat
      r = get random number
      until r is a new unique number
      remember you've seen r before

      Implementation is left as an exercise to the reader

      Help me! I'm turning into a grapefruit! Buzzwords!

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

      that may take forever. :~

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      P B 2 Replies Last reply
      0
      • L Luc Pattyn

        that may take forever. :~

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #5

        An easier way would be to reseed the randomizer after every call with a value greater than the last returned result. It's not a great way, but it cuts out the checking.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        L M 2 Replies Last reply
        0
        • P Pete OHanlon

          An easier way would be to reseed the randomizer after every call with a value greater than the last returned result. It's not a great way, but it cuts out the checking.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

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

          When N "random" numbers in [0, RANGE) need to be unique, the range typically is small, so I prefer to put them all in a bag and use a random index to get them, one by one. So there is no need for a retry. And the problem has no solution for N>RANGE so some precautions need to be taken in any algorithm based on retrying. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          B 1 Reply Last reply
          0
          • P Pete OHanlon

            An easier way would be to reseed the randomizer after every call with a value greater than the last returned result. It's not a great way, but it cuts out the checking.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            M Offline
            M Offline
            molesworth
            wrote on last edited by
            #7

            Would that work? Since the next random value could be less than the seed, you could re-seed with a value you've used previously.

            There are three kinds of people in the world - those who can count and those who can't...

            P 1 Reply Last reply
            0
            • M molesworth

              Would that work? Since the next random value could be less than the seed, you could re-seed with a value you've used previously.

              There are three kinds of people in the world - those who can count and those who can't...

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #8

              What I was thinking was actually using Random.Next with the starting value being the value you've just retrieved + some small amount as the starting point, e.g. Random.Next(lastVal + 1, lastVal + 100).

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              L M 2 Replies Last reply
              0
              • P Pete OHanlon

                What I was thinking was actually using Random.Next with the starting value being the value you've just retrieved + some small amount as the starting point, e.g. Random.Next(lastVal + 1, lastVal + 100).

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

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

                I'm afraid there is no way you can tell the RNG what all the numbers are that have already been picked, so the best you can achieve is avoid repeating the previous number, not all the older ones. Of course if all the OP wants to avoid is consecutive duplication, then your way would be fine. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                1 Reply Last reply
                0
                • L Luc Pattyn

                  that may take forever. :~

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  B Offline
                  B Offline
                  benjymous
                  wrote on last edited by
                  #10

                  Yup, but the OP didn't really specify enough to say what he actually wanted, so I gave a generic, if not necessarily useful answer.

                  Help me! I'm turning into a grapefruit! Buzzwords!

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    When N "random" numbers in [0, RANGE) need to be unique, the range typically is small, so I prefer to put them all in a bag and use a random index to get them, one by one. So there is no need for a retry. And the problem has no solution for N>RANGE so some precautions need to be taken in any algorithm based on retrying. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    B Offline
                    B Offline
                    benjymous
                    wrote on last edited by
                    #11

                    You could also shuffle your bag and just pop the top entry each time, depending on where the bottleneck ends up being

                    Help me! I'm turning into a grapefruit! Buzzwords!

                    L 1 Reply Last reply
                    0
                    • B benjymous

                      You could also shuffle your bag and just pop the top entry each time, depending on where the bottleneck ends up being

                      Help me! I'm turning into a grapefruit! Buzzwords!

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

                      That would take a Random Shuffle Generator, which isn't provided by the .NET Framework as of now. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      1 Reply Last reply
                      0
                      • P Pete OHanlon

                        What I was thinking was actually using Random.Next with the starting value being the value you've just retrieved + some small amount as the starting point, e.g. Random.Next(lastVal + 1, lastVal + 100).

                        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                        My blog | My articles | MoXAML PowerToys | Onyx

                        M Offline
                        M Offline
                        molesworth
                        wrote on last edited by
                        #13

                        Pete O'Hanlon wrote:

                        Random.Next(lastVal + 1, lastVal + 100)

                        Erm, doesn't that just give you a monotonically increasing set of values?

                        There are three kinds of people in the world - those who can count and those who can't...

                        P 1 Reply Last reply
                        0
                        • V vasavi p

                          Need the randomization of numbers without repeatation using c#

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #14

                          People ask that frequently here, have you searched the threads in this forum?

                          1 Reply Last reply
                          0
                          • M molesworth

                            Pete O'Hanlon wrote:

                            Random.Next(lastVal + 1, lastVal + 100)

                            Erm, doesn't that just give you a monotonically increasing set of values?

                            There are three kinds of people in the world - those who can count and those who can't...

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #15

                            Yes - I'm not saying it's the way I'd do it, just that it is a way.

                            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                            My blog | My articles | MoXAML PowerToys | Onyx

                            1 Reply Last reply
                            0
                            • V vasavi p

                              Need the randomization of numbers without repeatation using c#

                              C Offline
                              C Offline
                              cmk
                              wrote on last edited by
                              #16

                              How many non-repeating random numbers are you going to want to get ? What is the range of random numbers - are they integers or real.

                              ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

                              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