Generate Not Repeated Random Integer Values
-
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 :)
-
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 :)
-
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 :)
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 )
-
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.
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 )
-
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 )
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.
-
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.
how many teachers are going to get their students to run this code on a sample size that large though ;-)
-
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.
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 )