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. Generate Not Repeated Random Integer Values

Generate Not Repeated Random Integer Values

Scheduled Pinned Locked Moved C#
helptutorialquestionlounge
7 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.
  • E Offline
    E Offline
    Edwin Syarief
    wrote on last edited by
    #1

    Hi everyone :) Is anybody can help me, I have a problem here , how to generate not repeated random integer values ? Thanx in advance

    Regard, Edwin :)

    G C 2 Replies Last reply
    0
    • E Edwin Syarief

      Hi everyone :) Is anybody can help me, I have a problem here , how to generate not repeated random integer values ? Thanx in advance

      Regard, Edwin :)

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      edwin46, What do you mean by "generate not repeated random integer values"? Doesn't Random work for you? http://msdn2.microsoft.com/en-us/library/system.random.aspx[^] Regards, Gareth.

      C 1 Reply Last reply
      0
      • E Edwin Syarief

        Hi everyone :) Is anybody can help me, I have a problem here , how to generate not repeated random integer values ? Thanx in advance

        Regard, Edwin :)

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

        I need to write an article, this gets asked so often. You create a list of numbers, which is the range of numbers you want to choose from ( such as 1-100 ). You generate a random number between 0 and 99 ( for example ). You pull the number at that index out, and remove it from the list. Next time, you generate a number between 0 and 98, and do the same. And so on. The other option is to write a custom sorting routine that returns a random number, but I've never tried that, I'm not sure if it wouldn't end up going for ever as the sort order keeps changing.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        V 1 Reply Last reply
        0
        • G Gareth H

          edwin46, What do you mean by "generate not repeated random integer values"? Doesn't Random work for you? http://msdn2.microsoft.com/en-us/library/system.random.aspx[^] Regards, Gareth.

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

          I assume he means he wants the random numbers to not repeat. 4 2 3 1 5, not 4 2 1 4 5

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          1 Reply Last reply
          0
          • C Christian Graus

            I need to write an article, this gets asked so often. You create a list of numbers, which is the range of numbers you want to choose from ( such as 1-100 ). You generate a random number between 0 and 99 ( for example ). You pull the number at that index out, and remove it from the list. Next time, you generate a number between 0 and 98, and do the same. And so on. The other option is to write a custom sorting routine that returns a random number, but I've never tried that, I'm not sure if it wouldn't end up going for ever as the sort order keeps changing.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            V Offline
            V Offline
            Vikram A Punathambekar
            wrote on last edited by
            #5

            Christian Graus wrote:

            You create a list of numbers, which is the range of numbers you want to choose from ( such as 1-100 ). You generate a random number between 0 and 99 ( for example ). You pull the number at that index out, and remove it from the list. Next time, you generate a number between 0 and 98, and do the same. And so on.

            Hey, Christian, your technique works very well if the sample size is small and many of those numbers will be used (for instance, you want 15 unique random numbers less than 25). If the sample size is large ( 0 - 10 million ) and very few of them will be used, you are better off generating numbers within the range and checking if they have already been generated (you have to maintain a list for that). If not, you add the number to the list and use it.

            Cheers, Vikram.


            Zeppelin's law: In any Soapbox discussion involving Stan Shannon, the probability of the term "leftist" or "Marxist" appearing approaches 1 monotonically. Harris' addendum: I think you meant "monotonously". Martin's second addendum: Jeffersonian... I think that should at least get a mention.

            R C 2 Replies Last reply
            0
            • V Vikram A Punathambekar

              Christian Graus wrote:

              You create a list of numbers, which is the range of numbers you want to choose from ( such as 1-100 ). You generate a random number between 0 and 99 ( for example ). You pull the number at that index out, and remove it from the list. Next time, you generate a number between 0 and 98, and do the same. And so on.

              Hey, Christian, your technique works very well if the sample size is small and many of those numbers will be used (for instance, you want 15 unique random numbers less than 25). If the sample size is large ( 0 - 10 million ) and very few of them will be used, you are better off generating numbers within the range and checking if they have already been generated (you have to maintain a list for that). If not, you add the number to the list and use it.

              Cheers, Vikram.


              Zeppelin's law: In any Soapbox discussion involving Stan Shannon, the probability of the term "leftist" or "Marxist" appearing approaches 1 monotonically. Harris' addendum: I think you meant "monotonously". Martin's second addendum: Jeffersonian... I think that should at least get a mention.

              R Offline
              R Offline
              Russell Jones
              wrote on last edited by
              #6

              how many teachers are going to get their students to run this code on a sample size that large though ;-)

              1 Reply Last reply
              0
              • V Vikram A Punathambekar

                Christian Graus wrote:

                You create a list of numbers, which is the range of numbers you want to choose from ( such as 1-100 ). You generate a random number between 0 and 99 ( for example ). You pull the number at that index out, and remove it from the list. Next time, you generate a number between 0 and 98, and do the same. And so on.

                Hey, Christian, your technique works very well if the sample size is small and many of those numbers will be used (for instance, you want 15 unique random numbers less than 25). If the sample size is large ( 0 - 10 million ) and very few of them will be used, you are better off generating numbers within the range and checking if they have already been generated (you have to maintain a list for that). If not, you add the number to the list and use it.

                Cheers, Vikram.


                Zeppelin's law: In any Soapbox discussion involving Stan Shannon, the probability of the term "leftist" or "Marxist" appearing approaches 1 monotonically. Harris' addendum: I think you meant "monotonously". Martin's second addendum: Jeffersonian... I think that should at least get a mention.

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

                Yes, that's probably true, I can't imagine needing a small number of unique values between 1 and 10 million, tho. I guess it's possible, and what you say is a good point.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                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