SolvedGetting Values 1 - 100 By Chance

Posts 1–6 of 6 · Page 1 of 1
Getting Values 1 - 100 By Chance
Hey there agin guys.

I'm making a list of items that can be only done by chance ( from 1% - 100% )
Is there any code to do it not by using a listbox? ( Adding items on the listbox by using the loop code hangs the program )
( Selecting the value 1 - 100 randomly isn't good )

Its good for games to have this kind of feature.
Just like Ragnarok. The monsters drop items by chance.
What you need is a random integer Function! With a few IF lines of code you can get it working.

Code:
    Public Function GetRandomNumber(ByVal Minimum As Integer, ByVal Maximum As Integer)
        Dim Rand As New Random()
        Dim RandomNumber As Integer = Rand.Next(Minimum, Maximum)
        Return RandomNumber
    End Function
I think you don't get it. What you posted is giving an integer randomly.
Its still easy to get the values 1-10. They're supposed to be rare to get.

Btw. I got an idea but it will make the program hang.
Its by using a listbox.

Adding value "1" 1 time
Adding value "2" 2 times
Adding value "3" 3 times
Adding value "4" 4 times
And so on until adding the value "100" 100 times.
The total items would be more than 4000.

So its rare to have the value 1 because it was only added once.
100 as the easiest to get because it has the highest number of times it was added.

That's the time on using that random code on the listbox.

Any ideas than this one?
You need to get more creative man, you can increase how rare it is to get by Increasing the Maximum.

Code:
    Public Function GetRandomNumber(ByVal Minimum As Integer, ByVal Maximum As Integer)
        Dim Rand As New Random()
        Dim RandomNumber As Integer = Rand.Next(Minimum, Maximum)
        Return RandomNumber
    End Function
TO MAKE SOMETHING VERY VERY RARE
Code:
If GetRandomNumber(0,10000) = 10 then
msgbox("You Win!")
else
msgbox("You Lose!")
End If
TO MAKE SOMETHING VERY VERY EASY TO GET
Code:
If GetRandomNumber(0,10000) < 9000 then
msgbox("You Win!")
else
msgbox("You Lose!")
End If
See what i did there, your chances to get a 10 are very low. All you need to do is increase the maximum for a lower chance.
@.Yuzuru
Going by what you want, iv come up with 2 methods.

Adding value "1" 1 time
Adding value "2" 2 times
Adding value "3" 3 times
Adding value "4" 4 times
And so on until adding the value "100" 100 times.
The total items would be more than 4000. 5050 to be precise

Method 1
Code:
    Public Function GetNumber() As Integer
        Dim tmp As New List(Of Integer)
        Dim Index As Integer = 0
        While Index < 100
            Index += 1
            For i As Integer = Index To 100
                tmp.Add(i)
            Next
        End While
        Return tmp(New Random().Next(0, tmp.Count))
    End Function
Method 2
If you wish to eliminate the value.
Store the list on your form
Code:
Dim tmp As New List(Of Integer)
And the function. Only when the list is empty, the loop is initiated.
Code:
    Public Function GetNumber() As Integer
        If tmp.Count = 0 Then
            Dim Index As Integer = 0
            While Index < 100
                Index += 1
                For i As Integer = Index To 100
                    tmp.Add(i)
                Next
            End While
        End If
        Dim RanNum As Integer = New Random().Next(0, tmp.Count)
        Dim Val = tmp(RanNum)
        tmp.RemoveAt(RanNum)
        Return Val
    End Function
Anytime it returns the random value, it'l get removed from the list. So for example by chance the rare value 1 gets returned, it'l no longer be in the list.
@consca,
I will be needing the result of the given number so I need another code than that. I would still thank your for your help.
@Pingo,
Seems I would be able to use the method 1.

If there are any better codes than that, I would like to see it. ( With the comment how does the code work )
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us