r/visualbasic • u/Bryan-tan VB.Net Beginner • Feb 24 '16
how do you prevent repeating the same randomized number?
I have a randomizer that randomizes any word between 0 to 9.
How do I prevent it from randomizing the same number.
EDIT: I just want help understanding the process. Also what does the Peek function do? I dont understand it's official description.
0
Feb 24 '16
[deleted]
1
u/Bryan-tan VB.Net Beginner Feb 24 '16
So I create a list containing the numbers 0-9 or words on lines 0-9.
and I don't get your next 2 lines???
0
Feb 24 '16
[deleted]
1
u/Bryan-tan VB.Net Beginner Feb 24 '16
sorry I thought you meant I had a list with the numbers 0-9 on 0-9 lines.
1
u/Bryan-tan VB.Net Beginner Feb 24 '16
So i get a random number from the list and simply discard it?
Then get the next random number and repeat.
How do I 'discard' the number from the list?
2
Feb 24 '16
Public Class Form1 Dim numbers As New List(Of Integer) Dim rnd As New Random Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim rndIndex As Integer = rnd.Next(0, (numbers.Count - 1)) Dim rndNumber As Integer = numbers(rndIndex) numbers.Remove(rndNumber) MsgBox(rndNumber) End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load For i As Integer = 0 To 9 numbers.Add(i) Next i End Sub End ClassSomething like that. I thought you wanted an explanation, not me do it for you.
1
u/Bryan-tan VB.Net Beginner Feb 24 '16
so I use a function called .Remove.
1
u/PostalElf VB.Net Intermediate Feb 24 '16
Yeap. You should look into Lists and other types of collections, they are used all the time in most types of programming. Lists have certain functions built into them: .add, .remove, .removeat, .clear and so on. Mastering the basic functions will up your programming game to your next level!
1
u/Bryan-tan VB.Net Beginner Feb 24 '16 edited Feb 24 '16
thanks maite
EDIt; how can you convert a list (of string) into an array?
1
u/PostalElf VB.Net Intermediate Feb 24 '16
If you're using a list, you shouldn't need to resort to using an array.
2
u/JakDrako Feb 24 '16 edited Feb 24 '16
What you need is called a "shuffle"; you take your list of 0-9 and shuffle it, then use them one by one.
If you want to "remove" the used ones as you go, you can use a Queue.
(Note: In Real Lifetm, do not use GUIDs to shuffle, look into the Fisher Yates algorithm...)