It's possible to randomized (A-Z) Letters ? |
|
Author |
Message |
MorrisinLA

|
Posted: Sun Mar 27 09:30:25 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
It's possible to randomized (A-Z) Letters ? . coz i'm making a game it
like a typing tutor, my problem is i don't know how to randomize the
alphabets .. can anyone give me some advice how to do it or code .. ?
Visual Studio207
|
|
|
|
 |
Rick

|
Posted: Sun Mar 27 09:30:25 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
> It's possible to randomized (A-Z) Letters ? . coz i'm making a game it
> like a typing tutor, my problem is i don't know how to randomize the
> alphabets .. can anyone give me some advice how to do it or code .. ?
It depends on what you are trying to do. If you just want to generate a
random letter of the alphabet, and if you don't mind repeated letters,
then this one-liner will do...
MsgBox Chr$(65 + Int(26 * Rnd))
Of course, you don't have to MessageBox the result; you can assign it to
Label, Print it somewhere, etc. On the other hand, if you need to
present each letter of the alphabet in random order, then you need more
than one line of code. Here is a generalized Subroutine that will take
an array and randomize its elements
Private Sub RandomizeArray(ArrayIn As Variant)
Dim x As Long
Dim RandomIndex As Long
Dim tmp As Variant
' Only if an array was passed
If VarType(ArrayIn) >= vbArray Then
' Loop through the array elements
For x = UBound(ArrayIn) To LBound(ArrayIn) Step -1
' Select another random array index
RandomIndex = Int((x - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
' And reassign its content to the current array member,
' swapping the current member value to the other spot
tmp = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(x)
ArrayIn(x) = tmp
Next
Else
' The passed argument was not an
' array; error handler goes here
End If
End Sub
First, however, to make this work in a truly random fashion EACH time
you run your program, you have to instruct it to randomize the calls to
the Rnd function that is used in the Subroutine. You do that be issuing
a Randomize statement in either Form Load event (or in Sub Main if you
choose to start your programs from there). Here is a sample call to the
Subroutine showing what you want
Private Sub Form_Load()
Randomize
' Place other Form Load event code here, if any
End Sub
Private Sub Command1_Click()
Dim x As Long
Dim letters(0 To 25) As String
' Create an array of letters
For x = 0 To 25
letters(x) = Chr$(65 + x)
Next
' Then randomize the array's elements
RandomizeArray letters
' Now, the letters in the array are stored
' in a random order, so just iterate through
' them one at a time; here I print them out
' directly on the form so that you can see them
For x = 0 To 25
Print letters(x);
Next
End Sub
Rick - MVP
|
|
|
|
 |
Steve

|
Posted: Sun Mar 27 09:44:11 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
> It's possible to randomized (A-Z) Letters ? . coz i'm making a game it
> like a typing tutor, my problem is i don't know how to randomize the
> alphabets .. can anyone give me some advice how to do it or code .. ?
>
You could try something like this.
Private Sub Form_Load()
Randomize
End Sub
Private Sub Command1_Click()
MsgBox Shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
End Sub
Private Function Shuffle(ByVal s As String) As String
Dim Uppr As Long, i As Long, Swp As Long
Dim Buffr As String
Uppr = Len(s)
For i = 1 To Uppr
Buffr = Mid$(s, 1, 1)
Swp = Int(Uppr * Rnd + 1)
Mid$(s, 1, 1) = Mid$(s, Swp, 1)
Mid$(s, Swp, 1) = Buffr
Next i
Shuffle = s
End Function
--
Steve Garman
|
|
|
|
 |
MikeD

|
Posted: Sun Mar 27 10:06:37 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
> It's possible to randomized (A-Z) Letters ? . coz i'm making a game it
> like a typing tutor, my problem is i don't know how to randomize the
> alphabets .. can anyone give me some advice how to do it or code .. ?
I'd use ASCII values. For A - Z, you want to generate a random number
within the range 65 to 90. If you want lower case letters, then it'd be 97
to 122. Use the Chr$() function to get the letter. To make your code easier
to read, you could define constants or use the Asc function. Here's an
example that just prints a random string of upper case letters to the
Immediate window.
Private Sub Form_Click()
Dim lASCII As Long
Dim lCounter As Long
Dim sLetters As String
Randomize
For lCounter = 1 To 10
lASCII = Int(Rnd * (Asc("Z") - Asc("A") + 1)) + Asc("A")
sLetters = sLetters & Chr$(lASCII)
Next
Debug.Print sLetters
End Sub
Now you could write:
lASCII = Int(Rnd * 26) + 65
As you can see, that line of code isn't nearly as clear as to what you're
doing. You could alternatively use the intrinsic constants vbKeyA and
vbKeyZ:
lASCII = Int(Rnd * (vbKeyZ - vbKeyA + 1)) + vbKeyA
For lower case letters, you could use any of the following:
lASCII = Int(Rnd * (Asc("z") - Asc("a") + 1)) + Asc("a")
lASCII = Int(Rnd * 26) + 97
lASCII = Int(Rnd * (vbKeyZ - vbKeyA + 1)) + (vbKeyA + 32)
--
Mike
Microsoft MVP Visual Basic
|
|
|
|
 |
Larry

|
Posted: Sun Mar 27 11:28:06 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
> It's possible to randomized (A-Z) Letters ? . coz i'm making a game it
> like a typing tutor, my problem is i don't know how to randomize the
> alphabets .. can anyone give me some advice how to do it or code .. ?
Here is yet another method to shuffle letters. Pass in any string of
characters to get back the same characters in random order:
<g>
LFS
Private Sub Form_Load()
Randomize
End Sub
Private Sub Command1_Click()
MsgBox Shuffle("ABCDE")
End Sub
Private Function Shuffle(ByVal Text As String) As String
Dim max As Long, idx As Long, pik As Long
max = Len(Text)
Shuffle = Space$(max)
For idx = 1 To max
pik = Int(Rnd * max) + 1
Mid(Shuffle, idx, 1) = Mid$(Text, pik, 1)
Mid(Text, pik) = Mid$(Text, pik + 1)
max = max - 1
Next
End Function
|
|
|
|
 |
Rick

|
Posted: Sun Mar 27 11:45:36 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
> > It's possible to randomized (A-Z) Letters ? . coz i'm making a game
it
> > like a typing tutor, my problem is i don't know how to randomize the
> > alphabets .. can anyone give me some advice how to do it or code ..
?
>
> Here is yet another method to shuffle letters. Pass in any string of
> characters to get back the same characters in random order:
>
> <g>
> LFS
>
>
> Private Sub Form_Load()
> Randomize
> End Sub
>
> Private Sub Command1_Click()
> MsgBox Shuffle("ABCDE")
> End Sub
>
> Private Function Shuffle(ByVal Text As String) As String
> Dim max As Long, idx As Long, pik As Long
>
> max = Len(Text)
> Shuffle = Space$(max)
> For idx = 1 To max
> pik = Int(Rnd * max) + 1
> Mid(Shuffle, idx, 1) = Mid$(Text, pik, 1)
> Mid(Text, pik) = Mid$(Text, pik + 1)
> max = max - 1
> Next
>
> End Function
Because some might find decrementing what looks like the upper limit for
the For-Next loop confusing, it might be clearer to eliminate the
max = max - 1
line. Here is slight reworking of your function that accomplishes
this...
Private Function Shuffle(ByVal Text As String) As String
Dim max As Long, idx As Long, pik As Long
max = Len(Text)
Shuffle = Space$(max)
For idx = 1 To max
pik = Int(Rnd * (max + 1 - idx)) + 1
Mid(Shuffle, idx, 1) = Mid$(Text, pik, 1)
Mid(Text, pik) = Mid$(Text, pik + 1)
Next
End Function
Rick - MVP
|
|
|
|
 |
Larry

|
Posted: Sun Mar 27 20:31:35 CST 2005 |
Top |
Visual Basic >> It's possible to randomized (A-Z) Letters ?
> Because some might find decrementing what looks like the upper limit for
> the For-Next loop confusing,
Yeah I actually thought about leaving max alone, or adding a separate
variable, but as far as confusing anyone, would it be any more confusing
than something like:
> pik = Int(Rnd * (max + 1 - idx)) + 1
But in the end, I decided that they can try the function out to see that it
works. If they didn't know that changing max would have no effect on
the loop structure, they they stand to learn something new!
LFS
|
|
|
|
 |
|
|