Hi there,
i think i have gotten into a bit of a fix...
here is the problem: i have used the random obj to generate random numbers between 1 to 90. some times when the application runs, it picks a number twice.
for example an output would be 2,8,34,2,67....
is it possible to make sure all numbers picked are only selected once
that is if i request for 10 ranadom numbers, each 10 digits would be unique... this is driving me nutts!!![]()
thanks 4 ur help
Slow.

Random Numbers again!!!
Steve Sampson
Another option may be to create an array of the numbers 1 to 10 (or 1 to 100, etc.) in order, then do a random 'sort' on them by picking 2 at random and swapping them. do that 100 times or so.
Then as you request random numbers go through the list one by one.
sof4king
Thanks !
You just made my day !
beigewy
I want a fixed range of numbers in a random order for two reasons:
1. 1-26 gives me the alphabet
2. I want to pull questions out of an array in a random order
I don't understand your comment >You didn't really want random numbers to begin with<
are you making a distinction between a set of randomised numbers and a randomised set of numbers
Mongkut
mhhough
Hi Slow,
One way would be to keep a list of previously generated numbers, whenever a number is generated check the list, if the number exists in the list generate another random number, check the list, and so on...
Andreas Brosten
A property of randomly generated number is that at times there will be two consecutive numbers.
You didn't really want random numbers to begin with.
Rajesh Prabhu. R
HTH
In your post about random numbers, you said 'hopefully it will work' but I wanted a random list of numbers in a range.
The following appears to work. The form has a listbox only.
Public
Class Form1 Dim r As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'load the numbers 1 to 26 into a list box Dim n As Integer = 0 Dim i As Integer = 0 For i = 0 To 25n = r.Next(0, 26) + 1
Do While ListBox1.Items.Contains(n)n = r.Next(0, 26) + 1
LoopListBox1.Items.Add(n)
Next i End SubEnd
Class--------------------------
I've tested this several times and it delivers the goods. I've also tried it in a sorted list box so it's easier to see.
What puzzles me is why
n = r.Next(0, 26) + 1
appears to return a number in the range 1 to 26 and not 1 to 27. Am I wrong
Mongkut
Zodan
PsychUK
VB6 or not, I like the elegance of your solution.
GLS_1985
I've not actually tried any of the coding but in the old days you had to pick random numbers using the Rnd() function (still supported) this gives you a random decimal number always less that 1 (ie. 0.646432) and can return 0. You simply multiply by the number of possablities and integer the number so Int(Rnd()*10) would return a random number between 0 and 9.
So to get a random uppercase letter:
Chr(Int(Rnd()*26)+65)
and a random lowercase letter:
Chr(Int(Rnd()*26)+97)
I think I would prefer this method, but that's because I'm still thinking like a VB6 programmer. Do it whichever way you think is best.
PUG666
A set of randomly occurring number will have consecutive repeating numbers.
ukgy
'I've not actually tried any of the coding '
Perhaps, as Renee suggests, I did not post my question in the right thread; but my original reason for posting was that I don't find HTS's code snippet works - but it looks right.
My own code example works on my machine but it looks wrong (to me). I was hoping someone with more experience might try both.
Mongkut
Ian Smith
Mongkut....
Could I ask a question
Do you just not want consecutive repeats or no repeats.... which is difficult if there are only 26 possibilities.
I fear the way you asked you question, or specified what your wanted (spec'ed) was ambiguous.
Robert Kihm
Hopefully this will work for you:
Dim r As New Random Dim n As Integer Dim i As Integer Dim a As New List(Of Integer) For i = 1 To 10 ' Get a random numbern = r.Next(1, 90)
' Make sure it's not already in the list Do While a.Contains(n)n = r.Next(1, 90)
Loop ' add it to our lista.Add(n)
NextThis works by getting a random number in a given range, looking to see if it's in the list of previous numbers, if it's not it adds it, otherwise it gets another random number and looks again.
Alternatively, if you want to replace the List with a listBox that would work too: use listBox1.Items.Contains instead.
A word of warning though - if you narrow the random number range down to a small subset, e.g. 1,10 instead of 1,90, this routine may take some time to run.
HTH
chantit
thanks all you've been a great help!!
Great Great help!!
Dhatri
anyhow:
"What puzzles me is why
n = r.Next(0, 26) + 1
appears to return a number in the range 1 to 26 and not 1 to 27. "
Well in that situation r.Next will return integers from 0 up to, but not including 26. see the msdn library for Rand.Next(Int32, Int32). So you end up with 1 to 26.
Your code works...
I like the random sorter technique:
Next' radnomly sort the array (we'll do it a few times)Next' array is now randomly sortedNext' nomally you implement this to Sort an Array. We use it to UnSort the array.rand = New Random' must return 0 if they are the same' otherwise -1 or 1 depending on wether x should come before y - we decide at randomReturn 1The sensible way is to have a list of the numbers, draw one at random and then remove it from the list so that it isn't drawn in the next loop:
Next' draw a number from the poolMe.ListBox1.Items.Add(draw)' remove it from the poolNext