Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Become a VB Expert!

Join 149,631 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,085 people online right now. Registration is fast and FREE... Join Now!





Non repeating random number generator

Generates non repeating values to the array

Submitted By: m2s87
Actions:
Rating:
Views: 6,291

Language: Visual Basic

Last Modified: January 3, 2007
Instructions: Use it like
Call mitte_korduv_suvaliste_numbritega_massiiv(array, max_nr)

you can test it with:
Dim numbrid(6) As String
Dim a As String

Call mitte_korduv_suvaliste_numbritega_massiiv(numbrid, 49)
a = Join(numbrid, ",")

Snippet


  1. Sub mitte_korduv_suvaliste_numbritega_massiiv(ByRef massiiv, ByVal max_nr)
  2.     On Error GoTo viga
  3.     Randomize
  4.      
  5.         vaartused = Split(lisa_massi(max_nr), ",")
  6.  
  7.         For i = LBound(massiiv) To UBound(massiiv)
  8.             Do
  9.                 sala = Int(Rnd() * (max_nr - 1) + 1)
  10.                 DoEvents
  11.             Loop While vaartused(sala) < 1
  12.            
  13.             vaartused(sala) = 0
  14.             massiiv(i) = sala
  15.         Next i
  16.     Exit Sub
  17. viga:
  18. End Sub
  19.  
  20. Function lisa_massi(nr)
  21.     If nr > 0 Then lisa_massi = nr & "," & lisa_massi(nr - 1)
  22. End Function

Copy & Paste


Comments


Schmit38 2007-12-21 12:05:39

For Those of you who prefer english: Public Class Form1 'Create empty string of 8 numbers Dim number(6) As String Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ReadingRandomMutuallyExclusiveArray(number, 49) 'Number of members End Sub Sub ReadingRandomMutuallyExclusiveArray(ByRef arraytorandomize, ByVal MaxNumber) Dim a As String Dim arrayused() As String Dim i As Integer Dim ArrayIndexUsed As Integer Randomize() ' ReDim Preserve arrayused(7) arrayused = Split(GeneratedNumberString(MaxNumber), ",") '----------------------------------------------------------------------------------------------------- For i = LBound(arraytorandomize) To UBound(arraytorandomize) Do ArrayIndexUsed = Int(Rnd() * (MaxNumber - 1) + 1) 'ArrayIndexUsed = Random Number Picked 'DoEvent Loop While arrayused(ArrayIndexUsed) < 1 arrayused(ArrayIndexUsed) = 0 'This sets picked out array value as zero arraytorandomize(i) = ArrayIndexUsed 'Massiv is second array a = Join(number, ",") 'number is picked out of arrayused and create new list of 8 TextBox1.Text = a & vbCrLf Next i Exit Sub End Sub Function GeneratedNumberString(ByVal RemainingNumberstoRandomize) 'Creates 8,7,6,5,4,3,2,1 If RemainingNumberstoRandomize > 0 Then GeneratedNumberString = RemainingNumberstoRandomize _ & "," & GeneratedNumberString(RemainingNumberstoRandomize - 1) If RemainingNumberstoRandomize = 1 Then RemainingNumberstoRandomize = 0 End Function End Class

Schmit38 2007-12-21 13:08:47

Schmit35@msn.com I cannot get Numbers (1-8) with MaxIndex(7) VAARTUSED.LENGTH = 9 Causes endless loop

ishtiyaq 2008-05-09 06:23:48

ass hole


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month