PDA

Click to See Complete Forum and Search --> : Random letters/numbers...


|2eM!x
Jun 4th, 2005, 07:16 PM
This will generate Random numbers and letters at will, i know theres another kind, i just like making my own stuff :)
Private Sub Command1_Click()
Call RndLetter(True)
End Sub

Private Function RndLetter(Numbers As Boolean)
Dim AscChr As String, ChrAsc As String, rnduplow As Integer
Randomize
rnduplow = Int((3 * rnd) + 1)
AscChr = Abs(Int(97 - 122) * rnd - 97)
ChrAsc = Chr(AscChr)
If rnduplow = 1 Then
MsgBox ChrAsc
ElseIf rnduplow = 2 Then
MsgBox UCase(ChrAsc)
ElseIf rnduplow = 3 Then
MsgBox Int((9 * rnd) + 1)
End If
End Function

And heres the way everyone else does it:

Public Function CreateRandomLetter(ByVal pblnUpperCase As Boolean) As String
Dim lngAsc As Long
Dim strChar As String
Randomize
lngAsc = vbKeyA + CLng(Rnd * (vbKeyZ - vbKeyA))
If pblnUpperCase Then
strChar = LCase$(Chr$(lngAsc))
Else
strChar = LCase$(Chr$(lngAsc))
End If
CreateRandomLetter = strChar
End Function

enjoy guys :wave:

edit**
and heres a way to generate random anything

Option Explicit

Private Sub Command1_Click()
Call RndAny
End Sub

Private Sub RndAny()
Dim AscChr As String, ChrAsc As String
Randomize
AscChr = Abs(Int(0 - 255) * Rnd)
ChrAsc = Chr(AscChr)
MsgBox ChrAsc
End Sub

Inuyasha1782
Jun 5th, 2005, 01:15 AM
You just saved me from spending 20 minutes running a forum search! Nice job :D

Nice password make too:

Private Sub Form_Load
Dim AscChr As String, ChrAsc As String
Randomize
AscChr = Abs(Int(0 - 255) * Rnd)
ChrAsc = Chr(AscChr)
Msgbox "Your password is " & ChrAsc
Do Until Pass = ChrAsc
Pass = Inputbox ("What is your password")
If Pass = ChrAsc Then
Exit Do
End If
Msgbox "Incorrect password, please try again"
Loop
End Sub

Like it though :)

|2eM!x
Jun 5th, 2005, 02:44 AM
yup, also change the 0's to 1's, you dont want nulls in there :)

mstic
Aug 18th, 2005, 07:52 PM
Went through some of my really old code. I wrote a little generator thing much similar to these and just wondered how it compared for speed.

I measured by ticks, 100 words at 1000 characters per word
Write was dumping the words to a txt file.

Using the RndLetter Ascii func was slowest:
ASC Speed: 2141 Write: 15
ASC Speed: 2156 Write: 31
ASC Speed: 2203 Write: 16

Using the CreateRandomLetter vbkey func:
Call Speed: 1828 Write: 15
Call Speed: 1906 Write: 32
Call Speed: 1906 Write: 16

Using my old code from highschool era:
Gen Speed: 359 Write: 16
Gen Speed: 375 Write: 15
Gen Speed: 359 Write: 16

Here's the code... No idea why it spanks the others so bad
And yes I know it looks bad and is huge... but it's fast I didn't bother to pretty it up.

I included the little bit I wrote to test the speeds as well (goes to debug window)


Public Function WordGen(ByVal Length As Single)
Length = Length * 2
For L = 1 To Length 'Tell It How long a Word
NumGen = Int((36 * Rnd) + 1) 'Magic Number 1 - 36
Value = NumGen 'Record Magic Number
Letter = ""
Select Case Value 'Change Number to a Word
Case Is = 1
Letter = "A"
Case Is = 2
Letter = "B"
Case Is = 3
Letter = "C"
Case Is = 4
Letter = "D"
Case Is = 5
Letter = "E"
Case Is = 6
Letter = "F"
Case Is = 7
Letter = "G"
Case Is = 8
Letter = "H"
Case Is = 9
Letter = "I"
Case Is = 10
Letter = "J"
Case Is = 11
Letter = "K"
Case Is = 12
Letter = "L"
Case Is = 13
Letter = "M"
Case Is = 14
Letter = "N"
Case Is = 15
Letter = "O"
Case Is = 16
Letter = "P"
Case Is = 17
Letter = "Q"
Case Is = 18
Letter = "R"
Case Is = 19
Letter = "S"
Case Is = 20
Letter = "T"
Case Is = 21
Letter = "U"
Case Is = 22
Letter = "V"
Case Is = 23
Letter = "W"
Case Is = 24
Letter = "X"
Case Is = 25
Letter = "Y"
Case Is = 26
Letter = "Z"
Case Is = 27
Letter = "1"
Case Is = 28
Letter = "2"
Case Is = 29
Letter = "3"
Case Is = 30
Letter = "4"
Case Is = 31
Letter = "5"
Case Is = 32
Letter = "6"
Case Is = 33
Letter = "7"
Case Is = 34
Letter = "8"
Case Is = 35
Letter = "9"
Case Is = 36
Letter = "0"
Case Is = ""
Letter = ""
Case Else
Letter = ""
End Select
Word = Word & Letter 'Record Letter
Letter = ""
NumGen = "" 'Clear Place Holder
L = L + 1 'Record that one Letter is Done
Next L 'Advance to Next Letter
WordGen = Word 'Save word here
Word = "" 'Clear Word


I know its ugly and not very clean but it's lightning greasy chicken fast.

dragofire
Feb 21st, 2006, 01:34 PM
Does anyone know how to gernerate a set of numbers,so if I am was trying to use list of 25 numbers and I wanted to find out every possible combination of the 25 numbers, in sets of 7. example
1 2 3 4 5 6 7

7 5 4
1 2 6
7 2 5
1 7 4
so on and so on,

not sure if this is really a random issue, but I couldn't think of a better way to discibe it. any help would be appreciated. Thanks

Hiroshi
Mar 23rd, 2006, 10:22 AM
Well, to me, this is what it looks like you are trying to do. You want to find every possible combination of the(7 or 25 numbers) numbers. A combination tells you how many possible combinations that are possible. (nCr). and nCr = n!/(n-r)!/r! where n is the total number of values to choose from and where r is the number of values you choose. In your demonstration it would be 7C3 = 7!/5!/2! This doesn't really help,, but I thought what the heck, might as well post it...

Choober
Jan 15th, 2009, 06:55 PM
what do i add to make it? as in textboxes?

oh by the way is there one just like this for vb2008?

:wave:

HitchCraft
Jan 16th, 2009, 06:54 AM
Const MyString = _
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

Private Sub Command1_Click()
Text1.Text = RndWord(10)
End Sub

Private Function RndWord(Optional WordLength As Integer = 8) As String
Dim TempWord As String
Dim loopvar As Integer
For loopvar = 1 To WordLength
TempWord = TempWord & Mid(MyString, Int(Rnd * 62) + 1, 1)
Next loopvar
RndWord = TempWord
End Function

Private Function RndChar() As String
RndChar = Mid(MyString, Int(Rnd * 62) + 1, 1)
End Function



How does something like this stack up speedwise?
(this code requires a text box and a command button)