randomization of numbers without duplication in C#
-
vasavi.p wrote:
Need the randomization of numbers without repeatation using c#
Good. You've got the beginnings of a specification there; it needs fleshing out, but it's a decent start. What have you accomplished so far?
"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.
-
here's some pseudocode
repeat
r = get random number
until r is a new unique number
remember you've seen r beforeImplementation is left as an exercise to the reader
Help me! I'm turning into a grapefruit! Buzzwords!
-
here's some pseudocode
repeat
r = get random number
until r is a new unique number
remember you've seen r beforeImplementation is left as an exercise to the reader
Help me! I'm turning into a grapefruit! Buzzwords!
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.
-
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.
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.
-
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.
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.
-
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.
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...
-
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...
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.
-
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.
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.
-
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.
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!
-
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.
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!
-
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!
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.
-
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.
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...
-
People ask that frequently here, have you searched the threads in this forum?
-
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...
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.
-
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