I want to get random numbers between 0 to 100..But i do not want the random number to be repeated.

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim objRand As New Random
		objRand.Next(0, 100)
	End Sub

Suppose first time random numb generated is 10..Next time if same random number is generated,den i want to get the other random number..Can somebody help me in solving it???I want to get all the numbers b/w 1 to 100..but only one time

you can store each random no. in an array.
and when the next random no. is generated check whether it has been generated by checking the contents of the array.

Member Avatar for masterofpuppets

I suggest using a list to hold all the random numbers generated and every time the program generates a new number you just check whether that number is already in that list - if it is not, you just add it to the list, if it is already in the list, continue generating numbers untill the new number is not in the list :) that's what I use...

But if i want to take rando numbers b/w 0 to 1000.Perormances deceases

For large sets of numbers you could setup the list to be from smallest to biggest then check the middle number of the list.

If the middle number is higher (or lower) than the newly generated one then you know which half of the file its on... you can then take that half and repeat the same process until you get to a small enough number of elements (maybe set a min/max number of elements to have before stopping) to then check if the number is in that set.. if not then add it to the set.

takes a bit more logic but will improve performance when checking. It may be easier to do this in an array because you would then have the index numbers to work with when splitting the list.

Member Avatar for masterofpuppets

Ah yeah, this is a much better way of doing it :) ^

You're essentially looking for a card shuffle algorithm but you're using 101 cards.

This isn't VB but the principal logic is the same!

LOWCOUNT = 0
HIGHCOUNT = 100

NUM_COUNT = ((HIGHCOUNT - LOWCOUNT)+1)

Ary[ NUM_COUNT]
  // Fill with sequential numbers!
for i = 0; i < NUM_COUNT i++
     Ary[ i ] = LOW_COUNT + i

nCnt = NUM_COUNT
idx = 0
   
while (nCnt > 1)
{
      n = rnd( nCnt )           // Randomize remaining numbers
          // swap random #'s
      tmp = Ary[ idx ]
      Ary[ idx ] = Ary[ idx + n ]    // Copy random number
      Ary[ idx + n ] = tmp;
     nCnt--
    idx++
}
  // Last number already in position!

This uses a moving partition. N numbers are randomized by an index. The slots are filled from slot 0 to slot N using an incrementing destination index. As source index is randomized, the number in slot , the number in that index is saved in tmp. value in source index copied to destination index, tmp saved value stored in source index (they're swapped), destination index advanced, and number count decreased. When down to one remaining number, you're done!

Sounds complicated but very fast and very slick.

Thanks wildgoose. Good algorithm.
VB.NET array declaration syntax:

Dim  Array_var(UpperBound) As DataType
  'To create an array of 100 elements of Integer
  Dim Array_var(99) As Integer  ' 0 to 99 = 100 elements

Here is code,

Dim A(99) As Integer
        Dim i, j, t, k As Integer
        Dim rnd As New Random
        'Fil an array elements 
        For i = 0 To A.GetUpperBound(0)
            A(i) = i
        Next
        k = 0
        
        '   \ is integer division operator.
        '   While loop will iterate  - 99\4 =  24 times
        While k < A.GetUpperBound(0) \ 4
            'Exchange elements of two random index
            i = rnd.Next(100)
            j = rnd.Next(100)
            t = A(i)
            A(i) = A(j)
            A(j) = t
            k = k + 1
        End While

        Dim s As String
        s = ""
        For i = 0 To A.GetUpperBound(0)
            s = s & " " & A(i)
        Next
        MsgBox(s)

Thanks, adatapost.
However you may want to tweak your algorithm.

The original poster (unless they were in error) indicated 0 to 100 thus a range of 101. (Unless they wrote what they wanted wrong! Then you are fine!)

It does make more sense however that the class assignment was an even range of 100 not 101.

I want to get random numbers between 0 to 100..But i do not want the random number to be repeated.

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim objRand As New Random
		objRand.Next(0, 100)
	End Sub

Suppose first time random numb generated is 10..Next time if same random number is generated,den i want to get the other random number..Can somebody help me in solving it???I want to get all the numbers b/w 1 to 100..but only one time

U just take a array of size 100 or whatever you want and go on putting the number which u have generated randomly into the array provided checking weather the number is existing in the array or not. it may be lengthy b'coz u need to implement a search algorithm. But this works. i have done the same thing i done it for only 10 numbers.

hi SANDYPP, Instead of putting array elements into the array & checking it whether the element, is also present or not ,There is also another appraoch to do it,put the numbers from 0 to 100 into list..Suppose first time rand number returned is 98,remove this element from list.I thk so this is the easiest soln i found-

Dim list As New ArrayList
              For i As Integer = 1 To 10
    'Add the numbers to the collection.
                  list.Add(i)
              Next i
   
    Dim rand As New Random
    Dim index As Integer
    Dim item As Object
    'Display the items in random order.
              While list.Count > 0
    'Choose a random index.
                    index = rand.Next(0, list.Count)
    'Get the item at that index.
                  item = list(index)
    'Remove the item so that it cannot be chosen again.
                  list.RemoveAt(index)
    'Display the item.
                  MessageBox.Show(item.ToString())
               End While
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.