[RESOLVED] dim Array(50) . array (x), random number
I want to add number of numbers to be randomize from user not from the code
So the code is: dim Array (1 to 50) as long
I want to change 50 by 20 or any other no. while run time
My idea is to put x instead of 50 and the user add 50 from text or inputbox
Text1.text =x
But the array(x) could not be worked, must be number
Please help me adjust the code
Thank you
Re: dim Array(50) . array (x), random number
Welcome to the forums.
If you create a static array then you cannot resize it. If you create a dynamic array, then you can resize.
Recommend spending some time in this FAQ: What are arrays and how do I use them
Re: dim Array(50) . array (x), random number
Dear friend
Thank you very much for your help
I want code for randomize numbers from 1 to x which can be chosen from the user like 20 or 50 or whatever, but without repeat the number
Thank you again and God Bless you
Re: dim Array(50) . array (x), random number
If the user chooses, say, 20 random numbers, what kind of random numbers do you want? 1 to 20? decimals? What are maximum and minimum?
Re: dim Array(50) . array (x), random number
Quote:
Originally Posted by
MartinLiss
If the user chooses, say, 20 random numbers, what kind of random numbers do you want? 1 to 20? decimals? What are maximum and minimum?
non-repeating Random numbers from 1 to any number of numbers chosen by user who is using the program during run time,
the user want from 1 to 50 numbers or to 20 or to 10 numbers for examples, I want a vb6 code for that.
Re: dim Array(50) . array (x), random number
Go LINQ, go LINQ!
Code:
Public Function GetRandomArray(ByVal length As Integer) As Integer()
Dim rng As New Random
Return Enumerable.Range(1, length).OrderBy(Function(i) rng.NextDouble()).ToArray()
End Function
Create a range of numbers from 1 to length, order them randomly and then convert the list to an array. E.g.
Code:
Dim arr As Integer() = GetRandomArray(20)
For Each i As Integer In arr
MessageBox.Show(i.ToString())
Next
Re: dim Array(50) . array (x), random number
Quote:
Originally Posted by
jmcilhinney
Go LINQ, go LINQ!
Code:
Public Function GetRandomArray(ByVal length As Integer) As Integer()
Dim rng As New Random
Return Enumerable.Range(1, length).OrderBy(Function(i) rng.NextDouble()).ToArray()
End Function
Create a range of numbers from 1 to length, order them randomly and then convert the list to an array. E.g.
Code:
Dim arr As Integer() = GetRandomArray(20)
For Each i As Integer In arr
MessageBox.Show(i.ToString())
Next
Guess you didn't see that this was VB6.
Re: dim Array(50) . array (x), random number
Quote:
Originally Posted by
MartinLiss
Guess you didn't see that this was VB6.
I guess I didn't, given that I got to this thread via a link posted in another thread in the VB.NET forum. I guess I assume that noone would be silly enough to post a request for help on an existing VB6 thread in the VB.NET forum. I guess the world is always able to surprise. :rolleyes:
Re: dim Array(50) . array (x), random number
Quote:
Originally Posted by
Mido
Random numbers from 1 to any number of numbers chosen by user who is using the program during run time,
the user want 50 numbers or 20 or 10 numbers like 1,2,3,4,....to 20 for examples, I want a vb6 code for that.
So if the user wants 20 numbers you just want the numbers 1 to 20 in random order?
BTW please don't post links to this thread from this or any other forum.
Re: dim Array(50) . array (x), random number
This is the code I have, and it is working fine,
but the problem in it that I want the user of the program, while it is in run time to choose the number from (1 to 20) or from (1 to 30) or any other number,
Dim myArray(1 To 52) As Long
but because as I know now it is static array, and can't be changed in run time, can anyone make a code like the code below but with what I said before, Thanks.
Code:
Private Sub Form_Load()
'assure random numbers don't
'repeat each run
Randomize
End Sub
Private Sub Command1_Click()
'Set the number of elements needed. This demo
'uses 1 to 52 to simulate a deck of cards.
Dim cnt As Long
Dim myArray(1 To 52) As Long
'lists are for info only
List1.Clear
List2.Clear
'fill the array with consecutive numbers from 1 to 52
For cnt = 1 To UBound(myArray)
myArray(cnt) = cnt
'debug/info only - not needed for routine
List1.AddItem cnt & vbTab & myArray(cnt)
Next
'randomize (suffle) the array values
RandomizeArray myArray
'debug/info only - not needed for routine
For cnt = 1 To UBound(myArray)
List2.AddItem cnt & vbTab & myArray(cnt)
Next
'this is what i need
Text1.Text = Val(myArray(1))
End Sub
Private Sub RandomizeArray(ArrayIn As Variant)
Dim cnt As Long
Dim RandomIndex As Long
Dim tmp As Variant
'only if an array was passed
If VarType(ArrayIn) >= vbArray Then
'loop through the array elements in reverse
For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
'select a random array index
RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
'cnt represents one array member
'index, and RandomIndex represents
'another, so swap the data held in
'myarray(cnt) with that in myarray(RandomIndex)
tmp = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(cnt)
ArrayIn(cnt) = tmp
Next
Else
'The passed argument was not an
'array; error handler goes here
End If
End Sub
Private Sub List1_Scroll()
'if List2 is scrolled, keep List1 in sync
List2.TopIndex = List1.TopIndex
End Sub
Private Sub List2_Scroll()
'if List1 is scrolled, keep List2 in sync
List1.TopIndex = List2.TopIndex
End Sub
Re: dim Array(50) . array (x), random number
Code:
Option Explicit
Option Base 1
Private Sub Form_Load()
'assure random numbers don't
'repeat each run
Randomize
End Sub
Private Sub Command1_Click()
'Set the number of elements needed. This demo
'uses 1 to 52 to simulate a deck of cards.
Dim cnt As Long
Dim myArray(0) As Long
Dim intHowMany As Integer
intHowMany = InputBox("How many")
ReDim myArray(intHowMany)
'lists are for info only
List1.Clear
List2.Clear
'fill the array with consecutive numbers from 1 to 52
For cnt = 1 To UBound(myArray)
myArray(cnt) = cnt
'debug/info only - not needed for routine
List1.AddItem cnt & vbTab & myArray(cnt)
Next
'randomize (suffle) the array values
RandomizeArray myArray
'debug/info only - not needed for routine
For cnt = 1 To UBound(myArray)
List2.AddItem cnt & vbTab & myArray(cnt)
Next
'this is what i need
Text1.Text = Val(myArray(1))
End Sub
Private Sub RandomizeArray(ArrayIn As Variant)
Dim cnt As Long
Dim RandomIndex As Long
Dim tmp As Variant
'only if an array was passed
If VarType(ArrayIn) >= vbArray Then
'loop through the array elements in reverse
For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
'select a random array index
RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
'cnt represents one array member
'index, and RandomIndex represents
'another, so swap the data held in
'myarray(cnt) with that in myarray(RandomIndex)
tmp = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(cnt)
ArrayIn(cnt) = tmp
Next
Else
'The passed argument was not an
'array; error handler goes here
End If
End Sub
Private Sub List1_Scroll()
'if List2 is scrolled, keep List1 in sync
List2.TopIndex = List1.TopIndex
End Sub
Private Sub List2_Scroll()
'if List1 is scrolled, keep List2 in sync
List1.TopIndex = List2.TopIndex
End Sub
Re: dim Array(50) . array (x), random number
I found the solution by myself
Code:
Private Sub Form_Load()
'assure random numbers don't
'repeat each run
Randomize
End Sub
Private Sub Command1_Click()
x = Val(Text2)
'Set the number of elements needed. This demo
'uses 1 to 52 to simulate a deck of cards.
Dim cnt As Long
ReDim myArray(1 To x) As Long
'lists are for info only
List1.Clear
List2.Clear
'fill the array with consecutive numbers from 1 to 52
For cnt = 1 To UBound(myArray)
myArray(cnt) = cnt
'debug/info only - not needed for routine
List1.AddItem cnt & vbTab & myArray(cnt)
Next
'randomize (suffle) the array values
RandomizeArray myArray
'debug/info only - not needed for routine
For cnt = 1 To UBound(myArray)
List2.AddItem cnt & vbTab & myArray(cnt)
Next
'this is what i need
Text1.Text = Val(myArray(1))
End Sub
Private Sub RandomizeArray(ArrayIn As Variant)
Dim cnt As Long
Dim RandomIndex As Long
Dim tmp As Variant
'only if an array was passed
If VarType(ArrayIn) >= vbArray Then
'loop through the array elements in reverse
For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
'select a random array index
RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
'cnt represents one array member
'index, and RandomIndex represents
'another, so swap the data held in
'myarray(cnt) with that in myarray(RandomIndex)
tmp = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(cnt)
ArrayIn(cnt) = tmp
Next
Else
'The passed argument was not an
'array; error handler goes here
End If
End Sub
Private Sub List1_Scroll()
'if List2 is scrolled, keep List1 in sync
List2.TopIndex = List1.TopIndex
End Sub
Private Sub List2_Scroll()
'if List1 is scrolled, keep List2 in sync
List1.TopIndex = List2.TopIndex
End Sub
Re: dim Array(50) . array (x), random number
to try this, add a listbox, textbox and a command button...
Code:
Private Sub Command1_Click()
Dim NumNew As Long
Dim NumExist As Long
Dim TotalNums As Long
Dim i As Integer, j As Long
Dim Exist As Boolean
TotalNums = Val(Text1.Text)
Randomize
List1.Clear 'clear listbox
NumNew = Int(Rnd * TotalNums) + 1 'generate random number
For i = 1 To TotalNums
Do
DoEvents
NumNew = Int(Rnd * TotalNums) + 1 'generate random number
For j = 0 To List1.ListCount - 1
NumExist = List1.List(j)
If NumNew = NumExist Then
Exist = True
Exit For
Else
Exist = False
End If
Next
If Exist = False Then
List1.AddItem NumNew
End If
Loop Until Exist = False
Next
End Sub