-
May 15th, 2009, 06:42 AM
#1
Simple Randomize Question
I am trying to select a random number using a command:
Randomize
(variable as integer) = Int (Rnd * #) + #
Actual Code:
Randomize
t = Int (Rnd * 7) + 0
I want the random number, if selected to not be able to be selected more than once, and once it has been selected and it will keep cycling until it finds a number that has not been selected.
I will post more of the code later, right now i just put down some key parts.
If you need to know more about the code or whatever, just reply or message me.
-
May 15th, 2009, 08:15 AM
#2
Re: Simple Randomize Question
From what I can tell your code is missing some key information. This is how to generate a random number.
Code:
Randomize
variable = Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)
As far as keeping it to regenerate the same number, that would depend on how many digits your number is going to be. You would have to pass the variable to possibly a function to test it against a stored list of used values.
-
May 15th, 2009, 11:34 AM
#3
Re: Simple Randomize Question
From what I can tell your code is missing some key information. This is how to generate a random number.
Code:
variable = Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)
What
xmrw:
You should add the timer to your randomize statement to make sure it actually generates a random number that is not the same every time you run the program.
Using 7 there will of course only be 7 numbers that can show up.
one way to test against dupes is to simply append the number to a string and test that string for the number using instr. Another way and in this case a better way would be to use an array to store your numbers generated and test them against the array.
Something like
Code:
Private Sub Command1_Click()
Dim PickedNumbers(1 To 7) As Integer
Dim RndNumber As Integer
Dim X As Integer
Dim Y As Integer
Dim Dupe As Boolean
Randomize Timer
For Y = 1 To 7
Do
Dupe = False
RndNumber = Int(Rnd * 7) + 1
For X = 1 To Y
If RndNumber = PickedNumbers(X) Then
Dupe = True
Exit For
End If
Next
If Dupe = False Then
PickedNumbers(Y) = RndNumber
End If
Loop While Dupe
Debug.Print PickedNumbers(Y)
Next
End Sub
Last edited by DataMiser; May 15th, 2009 at 11:43 AM.
-
May 15th, 2009, 12:27 PM
#4
Re: Simple Randomize Question
Or this:
Code:
Option Explicit
' written by szlamany
Dim x As Integer
Dim lngchoice(10) As Long
Dim lngrnd As Long
Private Sub Form_Load()
Randomize
For x = 1 To 10
lngchoice(x) = x
Next x
' We now have an array of 10 items, 1 to 10
lngrnd = Int(Rnd * 10) + 1
Debug.Print "First lngRnd="; lngrnd; " Choice="; lngchoice(lngrnd)
For x = lngrnd + 1 To 10
lngchoice(x - 1) = lngchoice(x) ' Move down the values above our call
Next x
' We now have an array of 9 items
lngrnd = Int(Rnd * 9) + 1
Debug.Print "Second lngRnd="; lngrnd; " Choice="; lngchoice(lngrnd)
For x = lngrnd + 1 To 9
lngchoice(x - 1) = lngchoice(x) ' Move down the values above our call
Next x
' We now have an array of 8 items
lngrnd = Int(Rnd * 8) + 1
Debug.Print "Third lngRnd="; lngrnd; " Choice="; lngchoice(lngrnd)
End Sub
-
May 15th, 2009, 06:42 PM
#5
Re: Simple Randomize Question
If the range of your random numbers is not within the 10000 or so, you could make easy use of a string function.
Code:
public picked as string
function NewRnd as integer
dim r as integer
do
r = RandomNumberInTheRangeYouWant 'like Rnd(32)+1 or so
loop until instr(picked, "/"+cstr(r)+"/") = 0
picked = picked + "/" + cstr(r) + "/"
NewRnd = r
end function
Any number picked so far is stored in the string like "/nn/" where n is the number.
If a new random number is created, instr() is used to check if it exists already in the string of picked numbers.
-
May 15th, 2009, 06:58 PM
#6
Re: Simple Randomize Question
This shuffles the deck, and is probably faster than string ops
-
May 18th, 2009, 08:27 PM
#7
Re: Simple Randomize Question
I'd just like to add some extra information about xmrw and I. We're both freshmen students of a high school course that teaches Visual Basic 6.0. Therefore, we have about a semester's worth of knowledge in VB Code, so we are not familiar with extremely complex coding.
During this course, our final assignment is to create a relatively large program, and we can do this project on anything we want. Personally, I chose to simulate the card game 'War,' while xmrw chose to simulate the game 'ConnectFour.'
So our current dilemma is to create a random number sequence without repeating numbers. I need to do this in order to simulate a random deck, with no more than one of each card. As a result, I have the program pick a number from 1 to 52 and it chooses a card accordingly. Now I am working on eliminating the possibility of repeating cards.
xmrw's problem has to do with the computer's turn when playing ConnectFour. He wants to make sure the computer doesn't pick a spot that was already chosen, either randomly or by the player.
If you need more information about our current projects, just ask.
-
May 18th, 2009, 11:21 PM
#8
Re: Simple Randomize Question
For the deck you can use a routine similar to what I showed in my earlier post, just change the numbers from 7 to 52 and make the variable public. Use the logic in that routine to create a Shuffle sub then deal the cards from the array in order.
-
May 19th, 2009, 12:01 AM
#9
Re: Simple Randomize Question
In one of my many searches I came across a connect 4 game that used a simple Min/Max algorithm as its AI. I believe I found this at PSC (Planet-source-code.com). A search of the site or your favorite search engine should reveal some interesting results.
However, for keeping track of the play area for connect 4, a simple two dimensional boolean array or a two dimensional byte array should be sufficient...
Good Luck
-
May 19th, 2009, 04:17 PM
#10
Re: Simple Randomize Question
Thank you all very much. Some of that code suggested is a little too difficult and complicated for me at the moment. I just used a GoTo, and it worked pretty well... For now.
My next problem is going to be something along the lines of after choosing a position (computer or person) , the computer/person will be able to move to the position aloft.
-
May 20th, 2009, 08:01 AM
#11
Re: Simple Randomize Question
Originally Posted by Shake926
I'd just like to add some extra information about xmrw and I. We're both freshmen students of a high school course that teaches Visual Basic 6.0. Therefore, we have about a semester's worth of knowledge in VB Code, so we are not familiar with extremely complex coding.
During this course, our final assignment is to create a relatively large program, and we can do this project on anything we want. Personally, I chose to simulate the card game 'War,' while xmrw chose to simulate the game 'ConnectFour.'
So our current dilemma is to create a random number sequence without repeating numbers. I need to do this in order to simulate a random deck, with no more than one of each card. As a result, I have the program pick a number from 1 to 52 and it chooses a card accordingly. Now I am working on eliminating the possibility of repeating cards.
xmrw's problem has to do with the computer's turn when playing ConnectFour. He wants to make sure the computer doesn't pick a spot that was already chosen, either randomly or by the player.
If you need more information about our current projects, just ask.
Dear Shake926, I've seen that problem appear multiple times on the forum, have a look at this post:
http://www.codeguru.com/forum/showth...ghlight=random
Imo, it's the best solution for deck shuffling.
Regards
JeffB
Tags for this Thread
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
|