|
-
Apr 2nd, 2003, 05:15 PM
#1
Thread Starter
Hyperactive Member
Need Help On a Random Function...
i have this code:
VB Code:
If Check.Value = 1 Then
Randomed = True
Randomize List.ListCount
RanVal = Int(List.ListCount * Rnd + 1)
MsgBox RanVal
End If
So what it (should) do is to make a random number
from 0 to max(list.listcount)
and so it does, but i noticed that if i had a small amount of items in the list then it could generate numbers like: 1,5,3,7,6,7,5,6,5,3,2,3,4,8,9,8,8,4,5,2,1,2 and so on,...
And as you see from my example, it repeats many numbers often
what i'd like it to do is to NOT choose the same number twice before it has generated all possible numbers first...
I mean, if i have 10 items in the list, then i want it to randomly select all of them before it starts repeating any already selected items ..like 2,4,6,8,10,1,3,5,7,9 and then it repeats...in another random order ofcourse..
Can anyone helpp me do this??
***************
Please use [highlight=vb] ..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
My Unfinished Projects and My working Programs
***************
-
Apr 2nd, 2003, 05:31 PM
#2
Fanatic Member
What if you just created an algorithm in an array or something that tracks all the numbers it has previously chosen, once it has chosen all of them reset the tracker.
-
Apr 2nd, 2003, 06:04 PM
#3
Thread Starter
Hyperactive Member
I think i need an example?
As you see, i've never worked with Random before, and arrays are neither one of my "favourites" hehe..as in, i dont know to much about arrays either...
So any example's would give great help !!
***************
Please use [highlight=vb] ..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
My Unfinished Projects and My working Programs
***************
-
Apr 2nd, 2003, 07:57 PM
#4
Thread Starter
Hyperactive Member
*ding*
Anyone?
Anyone with an example code, or a solution?
***************
Please use [highlight=vb] ..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
My Unfinished Projects and My working Programs
***************
-
Apr 2nd, 2003, 08:56 PM
#5
Fanatic Member
VB Code:
dim x(y) as Integer
dim i as Integer
'Setup(reset) array to 0 which will indicate False as in hasn't been chosen yet
for i = 0 to list1.listcount-1
Redim preserve x(i+1)
x(i)=0
next i
'Then whenever a random selection is done, mark the index
'in the array relative to it as 1 indicating it has been used
private sub ChooseItem_Click()
dim s as integer
'Get item selecteds index
s=list1.listindex
'Assign its index which is relative to the array the number 1
'to indicate it has been chosen.
x(s)=1
end sub
I could elaborate more but I hope you get the gist of this.
-
Apr 2nd, 2003, 09:03 PM
#6
Thread Starter
Hyperactive Member
Didnt fully get the gist of it....
If you could make an working example, then that would really make me learn and understand this..
Cause the "mark the index in array..." didnt make sense to me,,,
So if you couuld elaborate it ,then i'd be thankfull
***************
Please use [highlight=vb] ..your code.. [/highlight] when posting code!
When you have received the working answer to your question,
please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.
Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...
Please Answer All Questions With Working Code Examples...
My Unfinished Projects and My working Programs
***************
-
Apr 2nd, 2003, 09:15 PM
#7
PowerPoster
Ive seen several threads on this forum on the subject. Do a search for random and repeat. Here's an interesting one:
http://www.vbforums.com/showthread.p...=random+repeat
-
Apr 2nd, 2003, 09:31 PM
#8
Hyperactive Member
Instead of
Randomize List.ListCount
try
Randomize Timer
-
Apr 2nd, 2003, 11:09 PM
#9
PowerPoster
Here is an example that works for sure cause i just wrote it from scratch and tested it.
You need a form with 2 listboxes and 1 button, then paste all this code into your form.
This takes the number of items in list1 and creates a random number and then adds it to list2. It continues to add random numbers to list2 untill all possible random numbers have been added.. it then repeats the process. Simply change the Counter variable number to loop as many times as you want.
VB Code:
Private Sub Command1_Click()
Dim i As Integer, j As Integer, x As Integer, CurNum As Integer
Dim NumsUsed() As Integer, Used As Boolean, Counter As Integer
ReDim NumsUsed(0)'Initialize aray
Do
DoEvents
Do
Used = False
Randomize
CurNum = Int((List1.ListCount) * Rnd()) + 1'get random number
For j = 1 To UBound(NumsUsed)'loop thru random numbers allready added to see if this number has been used.
If NumsUsed(j) = CurNum Then
Used = True
Exit For
End If
Next
If Used = False Then 'if it hasnt been used, add it to the used list
ReDim Preserve NumsUsed(UBound(NumsUsed) + 1)
NumsUsed(UBound(NumsUsed)) = CurNum
End If
Loop Until UBound(NumsUsed) = List1.ListCount ' Loop until all the numers have been added
Counter = Counter + 1 'Change this to increase the number of loops
For x = 1 To UBound(NumsUsed) 'add the numbers to list2
list2.AddItem (NumsUsed(x))
Next
ReDim NumsUsed(0) 'reset the array to empty
Loop Until Counter = 2
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 10
List1.AddItem (i)
Next
End Sub
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Apr 3rd, 2003, 12:36 AM
#10
Hyperactive Member
Hi Arc
The routine works but you'll need to filter dupes
after the scramble
For List2
Code:
Private Sub Filter_Duplicates()
Dim i As Long, J As Long
With List1
For i = 0 To .ListCount - 1
For J = .ListCount To (i + 1) Step -1
If .List(J) = .List(i) Then
.RemoveItem J
End If
Next
Next
End With
End Sub
BTW Arc.....
That routine is just what i need for a program i'm working on!!
Thank you...If I could send a beer via cable....
-
Apr 3rd, 2003, 01:36 PM
#11
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|