|
-
Oct 21st, 2005, 07:06 AM
#1
Randomize numbers
Hi All,
I made an application where I can randomize numbers into Listboxes.
It works very good! But I have a few questions about this issue.
My first question is can I randomize numbers in Labels?
My second question is, how can I avoid that there are the same numbers into
my listboxes?
Thanks,
sparrow1
-
Oct 21st, 2005, 07:42 AM
#2
Hyperactive Member
Re: Randomize numbers
For a label it would be label1.text = CStr(RandomizeCode)
Check the listbox to see if the number already exists and if it does randomize another one.
-
Oct 21st, 2005, 10:50 AM
#3
Re: Randomize numbers
It would probably be fastest to put your random numbers into an array, and once you have all the ones you want in an array, then move them into the listbox. Searching and removing from a listbox will be slower than doing the same operation on an array, though it may be faster in .NET than it was in VB6.
My usual boring signature: Nothing
 
-
Oct 21st, 2005, 11:21 AM
#4
Re: Randomize numbers
Hey shaggy,
The random is verry fast in .Net so about that no problem!
Just how can I check 20 labels (the solution from dannywooly works also perfect for randomize numbers in labels) that they doesn't show the same number!
Thanks
-
Oct 21st, 2005, 11:54 AM
#5
Re: Randomize numbers
If you are talking about numbers that small, the slowest process would still be acceptable, but keeping the numbers in an array until you have the ones you want would be some unnoticeable amount faster.
That would also help for the labels.
Alternatively, I would suggest that you write a short function that checks all currently filled labels, and returns False if the number is already in a label, and otherwise returns true.
My usual boring signature: Nothing
 
-
Oct 21st, 2005, 12:13 PM
#6
Re: Randomize numbers
I'll explain it again, this application is for kids who learn to divide, add, multiply and substract. So it doesn't matter wich number is in the Label. The only thing does matters is that in the same excercise never comes the same excercise.
That's the reason why I want to check the labels!
-
Oct 21st, 2005, 12:17 PM
#7
Hyperactive Member
Re: Randomize numbers
Well, at the stage in the program where you are about to write a label, scan through all the current labels and check their text isn't the same as the text you are about to write. If it is then generate new text. As there are only 20 labels it wont take long.
Last edited by jo0ls; Oct 21st, 2005 at 12:22 PM.
-
Oct 21st, 2005, 12:27 PM
#8
Re: Randomize numbers
Can you give me a code how I can scan the current labels?
-
Oct 21st, 2005, 01:08 PM
#9
Hyperactive Member
Re: Randomize numbers
VB Code:
Dim aLabel As Label
For Each c As Control In Me.Controls
If c.GetType.Equals(aLabel.GetType) Then
aLabel = DirectCast(c, Label)
' aLabel is now one of the labels on the form
' so put testing code here to test aLabel.text
End If
Next
-
Oct 22nd, 2005, 01:34 AM
#10
Re: Randomize numbers
oh dear god, there's a better way of doing that:
VB Code:
Dim myLbl As Label
For Eact Ctl As Control In Me.Controls
If Typeof ctl Is Label Then myLbl = Ctl
Next Ctl
also if this is wrong, i didnt read the entire thread here, only the last couple of posts
-
Oct 27th, 2005, 04:55 AM
#11
Re: Randomize numbers
 Originally Posted by jo0ls
VB Code:
Dim aLabel As Label
For Each c As Control In Me.Controls
If c.GetType.Equals(aLabel.GetType) Then
aLabel = DirectCast(c, Label)
' aLabel is now one of the labels on the form
' so put testing code here to test aLabel.text
End If
Next
Hi jo,
I've tryed your code, but it didn't worked. I've got still the same numbers in some of my Labels.
Mayby there is an easyer way to check the labels, two by two for example, instead of the 20 labels in one time.
Wkr,
sparrow1
-
Oct 27th, 2005, 05:07 AM
#12
Re: Randomize numbers
best way to generate a duplicate free list of random number is to create an array like {0,1,2,3,4,5,6,7,8.......} and then shuffle the array.
shuffling...
VB Code:
dim i, x, temp as integer
dim rng as random = new random()
for j as integer = 0 to 1
i = arr.length - 1
while i > 0
x = rng.next(i)
temp = arr(x)
arr(x) = arr(i)
arr(i) = temp
end while
next j
I don't live here any more.
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
|