הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, December 13, 2014 9:01 PM
I need to generate a random number between 1 and 25, which I can very easily do. However, I need to exclude certain numbers within that range. Number...4, 13, 16 and 22 will be excluded. I read that I could use Random.Next(List.Count)and so far this is what I have..
Randomize()
Dim random As Random
random = New Random
Dim randomNumbers(2) As Integer
For x = 0 To 2
randomNumbers(x) = random.Next(1, 25)
Next
Array.Sort(randomNumbers)
Dim Number1 As Integer = random.Next(X, X)
Any help is greatly appreciated..Thanks
All replies (13)
Monday, December 15, 2014 8:47 AM ✅Answered
Hi Mypaperdue,
So I only want number 6,7 and 10 to appear for Number2.
Dim Number1 As Integer = random.Next(1, 5)
Dim Number2 As Integer = random.Next(6, 10)
From the article in MSDN: http://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
When using the Random.Next(minValue As Integer, maxValue As Integer), The exclusive upper bound of the random number returned.
So, I suggest you using random.Next(1,6) to get the number from 1 to 5. and random.Next(6,11) to get #6-10.
I suggest you judge if the number2 is in{6,7,10}.
Dim Number1 As Integer = random.Next(1, 6)
Dim Number2 As Integer = random.Next(6, 11)
Dim c() As Integer = {6, 7, 10}
While Not c.Contains(Number2)
Number2 = random.Next(6, 11)
End While
If you have any other concern regarding this issue, please feel free to let me know.
Best regards,
Youjun Tang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
Saturday, December 13, 2014 9:07 PM
I need to generate a random number between 1 and 25, which I can very easily do. However, I need to exclude certain numbers within that range. Number...4, 13, 16 and 22 will be excluded. I read that I could use Random.Next(List.Count)and so far this is what I have..
Randomize()
Dim random As Random
random = New Random
Dim randomNumbers(2) As Integer
For x = 0 To 2
randomNumbers(x) = random.Next(1, 25)
Next
Array.Sort(randomNumbers)Dim Number1 As Integer = random.Next(X, X)
Any help is greatly appreciated..Thanks
I would encourage you to NOT use Randomize - it's old and should be deprecated.
*****
If you know the scale of the numbers you want, then one way would be to create a List(Of Integer) and as you populate that list, check that it's not one of the ones you want to include. From there, use the Random's .Next to select from the list - obviously it won't select something that's not there. ;-)
Still lost in code, just at a little higher level.
:-)
Saturday, December 13, 2014 10:36 PM
OK I will try it..Thanks
Saturday, December 13, 2014 11:20 PM
Let's say I am using this method and have it generating 2numbers ranging from (1,10)
Number1 will only show #1-5 and Number2 will only show #6-10 but what if I want to exclude # 8&9 from being generated?? I mean if i leave it like it is below, 8&9 will appear within the range.
So I only want number 6,7 and 10 to appear for Number2.
Dim Number1 As Integer = random.Next(1, 5)
Dim Number2 As Integer = random.Next(6, 10)
Saturday, December 13, 2014 11:36 PM
So I only want number 6,7 and 10 to appear for Number2.
You need to create a list that you can select from.
Dim LowSequence() As Integer = {1,2,3,4,5}
Dim HiSequence() As Integer = {6,7,10}
Then use the random number you generate to select from the two lists, using (0,5)for the first (because there are five items in the list) and (0,3) for the second (because there are three items in the list).
Sunday, December 14, 2014 1:05 AM
Let's say I am using this method and have it generating 2numbers ranging from (1,10)
Number1 will only show #1-5 and Number2 will only show #6-10 but what if I want to exclude # 8&9 from being generated?? I mean if i leave it like it is below, 8&9 will appear within the range.
So I only want number 6,7 and 10 to appear for Number2.
Dim Number1 As Integer = random.Next(1, 5)
Dim Number2 As Integer = random.Next(6, 10)
Another way would be to use a structure like I showed in one of your other questions.
Still lost in code, just at a little higher level.
:-)
Sunday, December 14, 2014 2:10 PM
I need to generate a random number between 1 and 25, which I can very easily do. However, I need to exclude certain numbers within that range. Number...4, 13, 16 and 22 will be excluded. I read that I could use Random.Next(List.Count)and so far this is what I have..
Randomize()
Dim random As Random
random = New Random
Dim randomNumbers(2) As Integer
For x = 0 To 2
randomNumbers(x) = random.Next(1, 25)
Next
Array.Sort(randomNumbers)Dim Number1 As Integer = random.Next(X, X)
Any help is greatly appreciated..Thanks
I would encourage you to NOT use Randomize - it's old and should be deprecated.
*****
If you know the scale of the numbers you want, then one way would be to create a List(Of Integer) and as you populate that list, check that it's not one of the ones you want to include. From there, use the Random's .Next to select from the list - obviously it won't select something that's not there. ;-)
Still lost in code, just at a little higher level.
:-)
Randomize has no effect when used in conjunction with the .Net Random class.
It is a legacy holdover from pre .Net, that works with Rnd
thanks for any help
Sunday, December 14, 2014 2:15 PM
That's a fairly simple task with LINQ:
Public Class Form1
Dim r As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim allNumbers() As Integer = Enumerable.Range(1, 25).Except(New Integer() {4, 13, 16, 22}).ToArray
allNumbers = allNumbers.OrderBy(Function(x) r.NextDouble).ToArray
MsgBox(allNumbers.First)
End Sub
End Class
thanks for any help
Sunday, December 14, 2014 3:43 PM
Should 1 and 25 be included?
'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.' JohnWein
Multics
My Serial Port Answer
Tuesday, December 16, 2014 3:17 PM
Okay thanks for all the help..I will try them when i get home from work.
Tuesday, December 16, 2014 9:06 PM
Create a Range that contains only the valid value 1 to 25, except 4,13,16,22
Get the number of item in the range
Use Random.Next to get a random index in the range
Dim R As New Random
Dim Range = Enumerable.Range(1, 25).Except({4, 13, 16, 22})
'This will generate a random value: 1 to 25, except 4,13,16,22
Dim Value As Integer = Range(R.Next(1, Range.Count))
Tuesday, December 16, 2014 10:01 PM
Create a Range that contains only the valid value 1 to 25, except 4,13,16,22
Get the number of item in the range
Use Random.Next to get a random index in the range
Dim R As New Random Dim Range = Enumerable.Range(1, 25).Except({4, 13, 16, 22}) 'This will generate a random value: 1 to 25, except 4,13,16,22 Dim Value As Integer = Range(R.Next(1, Range.Count)
I think you mean:
Dim Value As Integer = Range(R.Next(Range.Count))
'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.' JohnWein
Multics
My Serial Port Answer
Wednesday, December 17, 2014 4:33 AM
Dim Number1 As Integer = random.Next(1, 6) Dim Number2 As Integer = random.Next(6, 11) Dim c() As Integer = {6, 7, 10} While Not c.Contains(Number2) Number2 = random.Next(6, 11) End While
This is perfect...Work like a charm. Thank you for all of your help..:-)