Results 1 to 12 of 12

Thread: Randomize numbers

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  4. #4

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  6. #6

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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!
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    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.

  8. #8

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Randomize numbers

    Can you give me a code how I can scan the current labels?
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  9. #9
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: Randomize numbers

    VB Code:
    1. Dim aLabel As Label
    2.         For Each c As Control In Me.Controls
    3.             If c.GetType.Equals(aLabel.GetType) Then
    4.                 aLabel = DirectCast(c, Label)
    5.                 ' aLabel is now one of the labels on the form
    6.                 ' so put testing code here to test aLabel.text
    7.             End If
    8.         Next

  10. #10
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Randomize numbers

    oh dear god, there's a better way of doing that:

    VB Code:
    1. Dim myLbl As Label
    2. For Eact Ctl As Control In Me.Controls
    3.   If Typeof ctl Is Label Then myLbl = Ctl
    4. Next Ctl

    also if this is wrong, i didnt read the entire thread here, only the last couple of posts

  11. #11

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: Randomize numbers

    Quote Originally Posted by jo0ls
    VB Code:
    1. Dim aLabel As Label
    2.         For Each c As Control In Me.Controls
    3.             If c.GetType.Equals(aLabel.GetType) Then
    4.                 aLabel = DirectCast(c, Label)
    5.                 ' aLabel is now one of the labels on the form
    6.                 ' so put testing code here to test aLabel.text
    7.             End If
    8.         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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  12. #12
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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:
    1. dim i, x, temp as integer
    2. dim rng as random = new random()
    3. for j as integer = 0 to 1
    4.     i = arr.length - 1
    5.     while i > 0
    6.         x = rng.next(i)
    7.         temp = arr(x)
    8.         arr(x) = arr(i)
    9.         arr(i) = temp
    10.     end while
    11. 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
  •  



Click Here to Expand Forum to Full Width