|
-
May 14th, 2005, 11:27 PM
#1
Thread Starter
Lively Member
Random numbers, but not the same. [GIVEN UP]
OK, I got this code:
VB Code:
Dim p1 As Integer = RandomNo.Next(0, 4)
Dim p2 As Integer = RandomNo.Next(0, 4)
Dim p3 As Integer = RandomNo.Next(0, 4)
Dim p4 As Integer = RandomNo.Next(0, 4)
Dim p5 As Integer = RandomNo.Next(0, 4)
What I want to do, is generate a random number between 0 and 4, yet make none of the choices the same. So what can I do to exclude the numbers from the previous P's as the numbers are generated? This is outside of the Event Procedures so I can't use any Loops to check and corect it. I haven't the slightest clue on how to go about this. Any help appreciated.
Last edited by martw; May 15th, 2005 at 03:47 AM.
Reason: Given Up.
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 14th, 2005, 11:37 PM
#2
Fanatic Member
Re: Random numbers, but not the same.
You mean they're declared public?
You could declare them public and assign them on the Load event and put your loop there.
-
May 14th, 2005, 11:41 PM
#3
Thread Starter
Lively Member
-
May 14th, 2005, 11:56 PM
#4
Thread Starter
Lively Member
Re: Random numbers, but not the same. [Try again]
VB Code:
p1 = RandomNo.Next(0, 4)
Do
p2 = RandomNo.Next(0, 4)
Loop Until p2 <> p1
Do
p3 = RandomNo.Next(0, 4)
Loop Until p3 <> p2 And p1
Do
p4 = RandomNo.Next(0, 4)
Loop Until p4 <> p3 And p2 And p1
Do
p5 = RandomNo.Next(0, 4)
Loop Until p5 <> p4 And p3 And p2 And p1
OK, now I got this code. Will it actually work as intended by assigning a value to each p where the same number is not repeated?
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 15th, 2005, 12:22 AM
#5
Fanatic Member
Re: Random numbers, but not the same. [Try again]
No because evaluates to a Boolean
And you could make something much neater if you would use an array.
Last edited by grilkip; May 15th, 2005 at 12:28 AM.
-
May 15th, 2005, 12:34 AM
#6
Thread Starter
Lively Member
-
May 15th, 2005, 01:10 AM
#7
Fanatic Member
Re: Random numbers, but not the same. [Try again]
Well, I'm still in my learning stage myself, but here's what I came up with:
VB Code:
Const MaxNumGrilkips As Integer = 3
Dim p(MaxNumGrilkips) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RandomNo As New Random
Dim I As Integer
Dim Location As Integer
Dim NewVal As Integer
For I = 0 To MaxNumGrilkips
Do
NewVal = RandomNo.Next(0, 5)
Loop Until Array.IndexOf(p, NewVal) = -1
p(I) = NewVal
Next
End Sub
Note that the upperbound is 5.
Last edited by grilkip; May 15th, 2005 at 01:57 AM.
-
May 15th, 2005, 03:46 AM
#8
Thread Starter
Lively Member
-
May 15th, 2005, 04:00 AM
#9
Re: Random numbers, but not the same. [GIVEN UP]
I'm still going to put in my two cents. What about declaring an array with all the possible combinations of 1234 (24 possible I think), then use a random number to select which element of the array to use?
-
May 15th, 2005, 06:09 AM
#10
PowerPoster
Re: Random numbers, but not the same. [GIVEN UP]
Hi,
You will find that virtually all questions have been previously covered in this forum. Have a look at my final post in
http://www.vbforums.com/showthread.php?t=338279
Now, usw the random number generator to fill the array rather than the textboxes and you're there.
BUT Are you seriously using your code to generate 4 random numbers which must be 1,2,3 or 4?????
If so, althought there is a theoretic probability if it requiring only 256 loops, the nature of Random is that you might get a loop that lasts for hours!!! This reasoning applies if you MUST generate ALL the numbers in a given range.
What you need to do then is to set up all your numbers in a 1 dimension array, e.g.
arrNumbers(1) = 1
arrNumbers(2) = 2 etc
(there are quicker ways of doing this; loops for larger ranges, array assignment within the declaration for smaller)
At the start, all the stored numbers will be the same as the index number of the array element.
Then if the random generated is 3 you make a note of the number held in arrNumbers(3) and then loop through the array from the top downwards, pushing all the contents of the elements 4 and greater down 1. Now from element 3 upwards, the number held in the array elements from 3 upwards will NOT be the same as the element index number.
You then reduce the random generator limit by 1
Repeat this until you are left with 1 array element only, which will contain the final number you want.
BTW NEVER give up on a coding problem. If you develop that habit you will greatly limit your progress. You may have to change your approach, but all problems are solvable one way or another - even if it means throwing away your computor
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 15th, 2005, 10:09 AM
#11
Thread Starter
Lively Member
-
May 15th, 2005, 12:44 PM
#12
Re: Random numbers, but not the same. [GIVEN UP]
Whoof, here's a possibility that might be an improvement on taxes code, though I have never proven that this is correct:
Create the 1D array as he said:
array(1)=1
array(2)=2
etc.
Get two random integers: valu and direction.
VB Code:
if array(valu) >0 then
'you've got your number
array(valu)=0
else
'Find a number.
'ON even values of direction, look up.
if direction mod 2 >0 then
do
valu+=1
'Need to set valu back to the begninning if it goes over the top of the array.
if array(valu)>0 then
array(valu)=0
exit do
end if
loop
else
'Do basically the same thing, but start with
valu-=1
'Set valu to the top of the array if it has gone below the base of the array.
'Much left out here, but same as earlier loop
end if
The basic idea is that you get a random number. If that slot in the array has not been used, use it (set the slot value to 0 or -1). If the slot has been used, look either forwards or backwards in the array for the next unused slot, and use that one.
If you are truly starting your search from a random spot in the array, then the next empty slot should be equally random. However, I have a feeling that this is not quite true. With a large array (say 100 items), if five consecutive slots were used, then the odds that the next 'random' number would be the next number above that slot becomes 6/100 when it should be 1/100. By looking either up or down, you cut this non-randomness in half, but the time taken to find a number is reduced.
This may not be an issue. In your case, with values 1,2,3,4, The first number has a 1 in 4 chance of being 1. However, if it isn't 1, then the second number has a 1 in 3 chance of being 1. The final number is NEVER random in this case, so perhaps any scheme to create a complete set of exclusive random numbers will not be random.
My usual boring signature: Nothing
 
-
May 15th, 2005, 03:01 PM
#13
PowerPoster
Re: Random numbers, but not the same. [GIVEN UP]
Hi martw
Understood. Best of luck.
Hi Shaggy Hiker
With repect, I don't think what you have there covers the problem. What you are suggestiing simply records the fact that a particular number has been already chosen. It does not stop the process from producing previously delivered numbers. Just for interest the following works.
VB Code:
TextBox1.Text = ""
Dim arrNumber() As Integer = New Integer() {1, 2, 3, 4}
Dim icount As Integer
Dim irand As Integer
Dim r As Random = New Random
Dim iTop As Integer
Do
iTop = arrNumber.GetUpperBound(0)
irand = r.Next(0, iTop)
TextBox1.Text &= arrNumber(irand).ToString & "; "
For icount = irand To arrNumber.GetUpperBound(0) - 1
arrNumber(icount) = arrNumber(icount + 1)
Next
ReDim Preserve arrNumber(iTop - 1)
If arrNumber.GetUpperBound(0) = 0 Then Exit Do
Loop
TextBox1.Text &= arrNumber(0).ToString
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 16th, 2005, 09:21 AM
#14
Re: Random numbers, but not the same. [GIVEN UP]
Well, we don't seem to be communicating very well, because I am using the technique I suggested for a much larger array (up to thousands) where each number can only be used once.
In this case, however, I think Wild Bill had the best suggestion. It won't scale well, but if that is not an issue, it will be faster and more random than either of our suggestions.
My usual boring signature: Nothing
 
-
May 16th, 2005, 09:47 AM
#15
PowerPoster
Re: Random numbers, but not the same. [GIVEN UP]
Hi Shaggy Hiker,
Sorry, I'm not making myself clear.
Both your and my sugestions will take up to any number required, but, unless I have misread your code 'cos I have not actually tried it, every time a new random number is produced your code will simply reject it. My code prevents the random process from producing a previously produced number, without affecting the random nature of the selection. I.E. It will take only 4 random loops to produce a complete result
I think you will find that by ensuring that only the minimum number of Random numbers have to be produced, my code will be faster than WildBill's, but it depends on whether or not a 24 case Select Case is faster than 4 loops through Random.
But this is only academic as martw is going another path.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 16th, 2005, 11:12 AM
#16
Re: Random numbers, but not the same. [GIVEN UP]
I assumed that what Wild Bill meant was that you create an array of 24 integers (all possible combinations of the four digits used once each). Then a single random call would select one of the 24, and that would be the order of the random numbers. This would be VERY fast, because it would consist of only 1 call to random, and a look-up table. In fact, you could expand on that to re-generate the 1-4 array very quickly.
My code would always provide one new random number in each run. I wrote out more for martw's benefit, which may have clouded the issue. The basic idea is that you create a list of the possible numbers. Then use Random to get a random spot in the list. If the spot has been used, you look either up the list or down the list, and take the next unused number. Thus for each call to random there are these steps:
1)If the number is new, use it. One if statement is all that is used to determine this.
2)If the number is not new, then one if determines whether to look up or down, then there is a loop that will end within n iterations (where n is the size of the array). The loop body contains a single if.
It is academic at this point, but for something with serious speed requirements, Wild Bill's solution has the edge because the bulk of the effort can be pre-computed. As for our solutions....I guess we'd have to test. My doubt is that the numbers returned from my code is truly random.
My usual boring signature: Nothing
 
-
May 16th, 2005, 04:59 PM
#17
PowerPoster
Re: Random numbers, but not the same. [GIVEN UP]
HI,
Right. I follow what you are saying. That is quicker than I thought but as you say is not truly random. It limits some choices to 1 either side of another randum number.
Wildbill's code may (we won't know unless we try it) be faster but would be very cumbersome for large numbers. I might give them both a try just to see. But not tonight (as Napoleon said!!!)
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 16th, 2005, 08:40 PM
#18
Re: Random numbers, but not the same. [GIVEN UP]
His code becomes unwieldly above about 4 numbers.
1:1
2:2
3:6
4:24
5:96
rule Nx = x * (N(x-1)) For all x>1. Assuming 1:1
You sure wouldn't do 5 or more without a program creating the combinations for you.
I was thinking about my solution more. It is fast, but randomness degrades MAY degrade severly. However, if you were to map chosen and non chosen numbers as light and dark zones in a bar, it is likely that you would see some really interesting patterns. I would predict that at first several points of light would appear, and each point would slowly expand. It may be that one point would reach some size, and appear to rapidly gobble the whole region as the increased probability of landing in that zone caused it to grow at a faster rate than surrounding smaller zones. This could potentially create a feedback loop that accelerated growth.
Ok, enough rambling. The technique is quick, and adequately random for my application, but it clearly is not truly random.
My usual boring signature: Nothing
 
-
May 17th, 2005, 01:16 AM
#19
Re: Random numbers, but not the same. [GIVEN UP]
True, you certainly wouldn't want more than 4 unique numbers. What about setting up a loop to act as kind of an odemeter to populate the array?
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
|