Generating Random Letters
Okay, here's a couple questions that should be pretty easy to answer. First, how can I generate random letters? And since I haven't really used loops before in the stuff I've made, how can I make a loop so that the program continuously just generates these random letters over and over? Or perhaps a timer would be involved? These should be simple but I'm just not sure how to do it right.
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Generating Random Letters In Vb 6.0
Hi all
Sorry should have explained this in more detail, here's what I want to do.
I have one form with 5 text boxes on it and need to generate random letters between A-F and random Numbers between 0-9
For Example: Text box 1 A0, Text 2 B7 Text 3 F9 Text 4 C6 Text 5 E8
So when the user clicks the command button, the program will generate random letters and numbers in each of the text boxes.
Could anyone maybe just point me in the direction with a couple of lines of code to perform this task.
Thanks for any help in advance
Cheers,
Steptoe
Random Letters(not A Question On How To Generate Random Letters)
Hi,
My question is pretty straight forward...basically I'm generating random letters a - z and numbers 0 - 9...
I also have a combobox with numbers from 1 to 10 (which will be used to determine the length of the string ,ie,user picks 5,which should produce a list of random letters and numbers in a listbox
ts7gx
s93ks
3js9f
sxs0f
and so on and so on )
This is where my question begins ..
instead of using something like this :
Code:
Option Explicit
Dim schars as string
Dim sChar as string
Dim lchar as string
Dim tchar as string
Dim pchar as string
Private Sub Command1_Click()
lstnames.Clear
For i = 1 To txtamount.Text ' used to generate 1 to whatever number is typed into txtamount.text
schars = Chr$(Asc("a") + Int(26 * Rnd()))
sChar = Chr$(Asc("0") + Int(9 * Rnd())) + 1
lchar = Chr$(Asc("0") + Int(9 * Rnd())) + 1
tchar = Chr$(Asc("a") + Int(26 * Rnd()))
pchar = Chr$(Asc("a") + Int(26 * Rnd()))
if cbolength.text = "5" then
lstnames.AddItem schars & sChar & lchar & tchar & pchar 'which should procude a five character string.
end if
Next i
End Sub
would there be an easier/neater way of making the string 5 characters/numbers long(for example)without having to concatenate each variable,or even use that many variables to begin with ?
Generating Multiple Letters In VBA
Edit by Moderator: Please use the [vb][/vb] vb tags when posting your code
Hi everyone
I am doing a VBA program that will take a bunch of information from an access database and generate a certain amount of letters upon the information.
There is a template .Word document which has fields such as <<Fname>> <<Lname>> <<Address>, etc etc and I use ord.ActiveDocument.Content.Find Text and Replace functions to substitute the right values from the database.
There is no problem is its only one letter, but i need to for example generate 170 letters.
I would like to open a word document (as I have done) and then have a letter for everypage. No one is going to save these document. They are basically going to generate it and if needed print it.
I have created a loop which will loop through the records in the database:
Code:
Set rs22 = db.OpenRecordset(sql_list)
Set objWord = CreateObject("Word.Application")
objWord.Application.Visible = True
If (rs22.EOF And rs22.BOF) Then
Do
objWord.Content.InsertFile myTempFile
With objWord.ActiveDocument.Content.Find
.Text = "<<PFirst>>"
.MatchCase = False
.Replacement.Text = rs22("First")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<PLast>>"
.MatchCase = False
.Replacement.Text = rs22("Last")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<SchoolName>>"
.MatchCase = False
.Replacement.Text = rs22("Name")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<SchoolStreet>>"
.MatchCase = False
.Replacement.Text = rs22("Address")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<SchoolCity>>"
.MatchCase = False
.Replacement.Text = rs22("City")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<SchoolState>>"
.MatchCase = False
.Replacement.Text = rs22("State")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<SchoolZipcode>>"
.MatchCase = False
.Replacement.Text = rs22("Zip")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<PlayerFirst>>"
.MatchCase = False
.Replacement.Text = rs22("[DIS:DSDSF]")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<PlayerLast>>"
.MatchCase = False
.Replacement.Text = rs22("[DIS:DSDSL]")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<PlayerCoach>>"
.MatchCase = False
Dim player_or_coach As String
If (rs22("[DIS:DSPOC]") = "P") Then
player_or_coach = "Player"
Else
player_or_coach = "Coach"
End If
.Replacement.Text = player_or_coach
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<Sport>>"
.MatchCase = False
.Replacement.Text = rs22("[COD:EDESC]")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<Conference>>"
.MatchCase = False
.Replacement.Text = rs22("CON_CFDSC")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<DNumber>>"
.MatchCase = False
.Replacement.Text = rs22("[DIS:DSNUM]")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<Date>>"
.MatchCase = False
.Replacement.Text = Format(rs22("[DIS:DSDTE]"), "mmmm d yyyy")
.Execute Replace:=2
End With
With objWord.ActiveDocument.Content.Find
.Text = "<<PB>>"
.MatchCase = False
.Replacement.Text = "^m"
.Execute Replace:=2
End With
rs22.MoveNext
Loop Until rs22.EOF
My question is:
Can i do like im doing in the last paragraph of code here and replacing <<PR>>, which is in the template at the bottom of the page, with the "^m" which is suppose to be the key code for a manual page break??
And after i do that can i do another:
objWord.Content.InsertFile myTempFile
So that on PAGE 2 this will load up my template file and i can modify the changes, and loop it through how ever many records we have. Can i do that?
I have been trying to find more information about this through google, but have been unsuccessful so far.
The template file is exactly one page long.
Thank you very much for your help
Mike
Random Generating
i am creating a program that i want to simulate a bit test with. so if i click on label.caption i want label14.caption to to go from the caption "bite" to a 5 sec delay during which it will display "bite in progress" and after the time period of 5 sec i want label14.caption to select at random a fault from a list i specify.
Generating A Random
Hello, i have a playlist editor for an MP3 player and would like to have a random song play function. I know how it would work but not sure about the code required to generate the random number values. I would need code to fetch how many entries there are in the listbox, then some code the generate a randon listindex number, any ideas?
Random Letters
Code:
Private Sub Command1_Click()
For i = 0 To 2
Randomize
ch = Int((26) * Rnd)
Label1.Caption = label1.caption & Chr$(ch + Asc("A")
Next i
End Sub
What i'm trying to do is make label1 have any 3 random characters but the Label1.Caption = label1.caption & Chr$(ch + Asc("A") part is wrong and i don't know what i'm doing wrong, can someone point out my mistake?
Ok sorry but one more thing how would i make it so i generate something then the next time it generates it's under the first thing example:
fwgh
wtee
sges
that are something with spaces between each like rere asda adsd etc. How would i do this? Is there a tutorial for this or something?
Random Letters
I know about random numbers (tutors corner helps a lot), but how do I generate a random letter?
Jacob Sheehy
http://www.sheehy.ca
Multitasking - screwing up several things at once
Random Letters
VB Code:
Dim strAccount As String, intPosition As Integer, strOutput As StringstrAccount = "TE[$$$$$]XT" For intPosition = 1 To Len(strAccount) Randomize Timer 'More Random results for Rnd. If Mid$(strAccount,intPosition,1) = "$" Then strOutput = strOutput & UCase$(Chr(Int(Rnd * 26)+ 97)) 'Random UpperCase Character A-Z Else strOutput = strOutput & Mid(StrAccount,intPosition,1) End IfNext text1.text = strOutput
Basically this does is makes text named TE[RANDOMLETTERSHERE]XT and shows it in the text box but how can i make it so theres two text boxes and it can replace the "TE and XK" in
VB Code:
strAccount = "TE[$$$$$]XT"
so i wouldnt have to go in the coding to change the name i would appreciate the help! thank you!
Random Letters
VB Code:
Private Sub Command1_Click() Dim A As Integer A = Int((100 - 1 + 1) * Rnd + 1) Text1.Text = A End Sub Private Sub Form_Load() Randomize End Sub
The above code ceates a random number between 1 and 100.
How can I do something similar to the above but make it create random letters from the alphabet?
Random Letters
can someone tell me how to add random letters to a textbox without repeating the same number
ive tried a few things but not happening for me
here i have commandbutton to produce the random letter a label1 to count how many clicks of command 1 happen and label2 to show which letters have been produced
i hope someone can help me
thanks
heres the code i have for generating the random text
VB Code:
Private randlbltxt As String[u]Private txtarray() As String [/u] Private Sub Command1_Click()Dim randtxt As String Label2.Caption = Label2.Caption & randlbltxt & vbNewLineText1.Text = randlbltxtLabel1.Caption = Label1.Caption + 1 randtxt = ",A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" txtarray = Split(randtxt, ",") randlbltxt = txtarray(Randomize(1, 26)) [u]End sub [/u] Public Function Randomize(ByVal Low As Long, _ ByVal High As Long) As Long Randomize = Int((High - Low + 1) * Rnd) + LowEnd Function
Random Letters!?!?
I am creating a bingo program. i have a lable for the random number, and for the random letter, I need my random letter to be random-b-i-n-g-o. but i am having some problems any advice would be much apreciated!
Random Letters?
is there a way to generate random letters in vb?
i did it by assigning each letter to a number (1=a, 2=b...) but i want to know if there's an easier way
Generating A Random Dungeon Map...
Programming slashers are pretty fun. Playing them are fun too. The one thing I have found that makes the games old quickly is "static maps".
I've done a few games with great "map editors", but after a while, creating "new maps" get's to be a pain and, besides, if you create the map yourself... where's the adventure in that?
About the most famous of ALL games in which has random map generators is Diablo 1 & 2. I guess that's why those games stay popular for so many years... the maps are always different for each game.
In doing this, I have discovered it's no easy task to "do a good map everytime".
Plain "Map editors" are easy and aren't much of a challenge. I'm looking for something to make a game engine that will keep interest.
I've done a couple of generators, but the results are no where near satisfying.
I am wondering if there is anyone here who has successfully coded a Random Map Generator?
I'm NOT looking for code... What I am looking for is "approach", or flow of your code.
I'm starting with simple maps. 2D Square Dungeon maps. Rooms and Hallways and Doors.
My approach is starting top left, doing 1 square at a time. Do some logic on the surrounding squares, make a decision on what to make that square, move on to the next.
The problem is there isn't much structure to the map.
How would you do it? Generate Hallways for the Map, then fill in the spaces with Room, then Doors?
Or, Generate Rooms throughout the map, then fill in with hallways?
...etc.
Just looking for some Code "FLOW" ideas.
Thanks!
Generating Random Numbers
I was wondering if anyone knew how to create a random number generater that would symbolize a deck of cards. I alread know how to generate the 52 numbers nut i don't know how to make the program use each number only once.
Generating A Random Number Between 1 And 10
What code can I use to generate a random number between 1 and 10. This is how it is gonna work... I have 10 quetions for my users:
Question1
Question2
Question3
Question4
Question5
Question6
Question7
Question8
Question9
Question10
and I need to generate 5 random numbers between 1 and 10. These random numbers will decide which 5 questions my program will ask them. so if one of the random numbers is 3 it will ask them question 3. Any ideas how I can do this?
Generating 7 DIFFERENT Random Integers
I need to generate 7 integers between 1 and 7 that are all different. Thanks.
By the way... This is the code I have so far (will only genertae the numbers, not make sure they are different)
Code:
Randomize()
z = Int(Rnd() * 7) + 1
y = Int(Rnd() * 7) + 1
x = Int(Rnd() * 7) + 1
w = Int(Rnd() * 7) + 1
v = Int(Rnd() * 7) + 1
u = Int(Rnd() * 7) + 1
t = Int(Rnd() * 7) + 1
[HELP] Generating Random Filenames
Hello there. I need some help generating random filenames.
I have tried:
Code:
Dim 1 As String
Dim 2 As String
dim 3 As String
3 = randomnumber
2 = ".ini"
1 = App.Path & "" & "3" & Extension
Print #1, "#Account DO-NOT-CHANGE-ANYTHING-HERE"
Close #1
Something like that, but the file wont save, and gives me an error. Can someone please help?
Generating Random Pictures
Ok, So i have a big picture box on a form which will work as the battle background for a turn based game I am making. I want it to load one of the random backdrop pictures i have on form load.
So heres the code I have made so far, but it does not seem to work. This is important because I dont want to have the same background on every fight and im not using multi forms just to battle with different backdrops.
Someone help
Code:
Const MAX_BACKGROUND As Integer = 10
Dim bgArray(MAX_BACKGROUND - 1) As String
Private Sub Form_Load()
InitBackground
SelectBackground
'This just displays a name they entered
nameShow.Caption = ReadINI("config", "CharName", App.Path & "config.ini")
End Sub
Private Sub InitBackground()
bgArray(0) = "backdrop1.jpg"
bgArray(1) = "backdrop2.jpg"
bgArray(2) = "backdrop3.jpg"
bgArray(3) = "backdrop3.jpg"
bgArray(4) = "backdrop3.jpg"
bgArray(5) = "backdrop3.jpg"
bgArray(6) = "backdrop3.jpg"
bgArray(7) = "backdrop3.jpg"
bgArray(8) = "backdrop3.jpg"
bgArray(9) = "backdrop3.jpg"
End Sub
Private Sub SelectBackground()
Dim RandomNumber As Integer
RandomNumber = Random(0, MAX_BACKGROUND)
Dim curPath As String
curPath = "graphics"
BattleScreenPicture.Picture = LoadPicture(curPath + bgArray(RandomNumber))
End Sub
Function Random(Lowerbound As Long, Upperbound As Long)
Randomize
Random = Int(Rnd * Upperbound) + Lowerbound
End Function
Help Generating A Random Number [vb 6]
hello
im looking for the old way of generating a random number, linking it to a variable. I remember it being something quite simple but i cant remember the exact code for it
thanks
Generating Random Numbers In VB6
For a project I have currently done, I have designed a database that contains a table, which contains the field "NHS Number". People in the UK would know that this is a unique ten digit number.
I am in the process of adding "people" into the database for testing purposes.
In real life, different people would have totally different NHS Numbers, so using AutoNumber is totally out of the question. So I have to have a random number...
I have currently made a random integer generator to solve this problem... or so I thought
Using the following code:
vb Code:
Private Sub Command1_Click() Dim dblRand As Double Dim strRand As String dblRand = Int(Rnd * 10000000000#) Text1.Text = dblRand Clipboard.Clear strRand = dblRand Clipboard.SetText strRandEnd Sub
Using the
Code:
Int()
command doesn't acutally generate a random number each time for me. While I'm running the program, it generates random numbers, fair enough. When I start the program again, it generates the same random numbers it did before
Is there something I've done wrong? Is there another way to randomise numbers?
Thanks
Generating A Random Number?
Is there anyway to make VB generate a random number.
eg.
Integer = Random(0,200)
so it would pick a number between 0 and 200
Generating Random Numbers
I am fairly new to vb. But I dont know how to go about this problem. What I need is to Generate Random Numbers between 0 and 1. Numbers cant be more than 3 decimal places (.000).
I need help on this ASAP. So if anyone can shed some light on this, it would be great.
If you can provide code it would be even better.
Thanks to all that reply.
Generating Random Strings?
I want to make a simple little app where you click a command button and it puts random text into a text box. So how can i set some text as a string and randomize it upon the command button click?
E.G:
I have these strings
This is a random text string 1
And so is this but random 2
I am also a random text string 3
I might be lucky and randomly picked to be put in the text box
If you get what i mean, now on the command button click one of the above strings are selected.
THANKS!
Generating Random Number
Hi everybody,
Anyone knows how to generating Random Integer Number beetwen 1 to 1000 ?
please tell me .....
Generating Random Arrays!!! HElp Please
OK, I have an array named
strArray()
it holds 50 different strings created dynamically.
How then can I tell VB to resort the array in a random order?
Thanks
Generating Random Numbers
In the Program I am Writing I need to have the ability to generate random numbers... Mainly 1-100 right now I used this
per1.Text = Int(Rnd * 100)
But it does not generate a very random number beig each time i run the program it runs the same numbers...
ButI also need to know how to generate random numbers for other TYPES of dice.. Such as a D3 D6 D10 and d20 But I do not even know where to start generating these? if anyone can give me a step in the right direction........
TIA
Rogan
Generating Random Number
Anyone know any code to make VB generate a random number between 1-100, 1-20, 1-10, and 1-6? For anyone who plays AD&D, i'm making a Magical Item Generating program.
Generating Random Times
Hi
Anybody got a good way of generating a random time, compare the result against another time and then act on it??
Cheers
Generating Random Number
Some one please guide me for generating the random number
the question is
Write a VB program to generate random numbers n where n is = 5, 10, 15 and 20
the user should select value of N from a combo box
and respective n random number should be generated.
thanx in advance
Generating Random Numbers
How does one go about generating a random number in VB? I'm interested in knowing how to generate one in two scenarios:
Generate random # between 1 and 298
Generate random # between 0 and infinity
Thanks for your time!
Random Letters And Numbers
how do i build functions...
1] to return only random letters
2] to return only rand numbers up to 9
its got something to do with rnd(), anyway thanks for any help
Replacing Random Letters With -
im making a game in which when the hint button is pressed, it will display
the first letter of the answer, and replace all the remaining letters with "-"
Ive tried the replace function, but you need to specify which items you want to replace, and i cant figure out how to do that.
Random Numbers And Letters
VB Code:
Function RandomN() As String Dim Y As Byte Y = Int((57 - 48 + 1) * Rnd + 48) RandomN = Chr$(Y) End Function Function RandomL() As String Dim Y As Byte Y = Int((122 - 97 + 1) * Rnd + 97) RandomL = Chr$(Y) End Function Private Sub Command1_Click() MsgBox RandomNL End Sub Private Sub Form_Load() Randomize End Sub
As You can see in Function RandomN and RandomL, random letters and numbers are generated. What I am trying to do here is Randomize RandomL and RandomN, which will generate random numbers + letters.
Example) MsgBox RandomL & RandomL & RandomN & RandomN
The above would maybe be random letters/numbers but RandomL and RandomN are not.
Any hints/adive would be helpful.
(a Twist On Random Letters)
Hi,
I have the following code to generate random letters:
VB Code:
Function RandomLetters() As String Dim sRndLetters As String Dim iCount As Integer Dim iChar As Integer Randomize For iCount = 1 To 11 Do Until (iChar >= 65 And iChar <= 90) Or (iChar >= 97 And iChar <= 122) iChar = Int(Rnd() * 123) DoEvents Loop sRndLetters = sRndLetters & Chr(iChar) iChar = 0 Next RandomLetters = sRndLettersEnd Function
...it works fine. Here's my question: how would I modify this code so that it only makes a single random set of letters per execution of the program? If that's not clear:
I'd like to use "RandomLetters" multiple times in this program, and I want the letters to be the same in all instances. However, next time I use this program, I'd like the letters to be different.
I hope I make sense. I usually don't.
Thanks!
Letters At Random (without Showing Twice)
Hi all, i use the code below to show letters of the alphabet at random.But what i'm looking for is never showing the same letter until the complete alphabet is past.What do i have to add in my code?
Private Sub Random()
Dim Alpha As String
Randomize
Alpha = Chr$(Int((90 - 65 + 1) * Rnd + 65))
Label1.Caption = Alpha
End Sub
Generating Non-Linear Random Numbers
I need to create random data but not on a linear scale. For example, I want random data to show how many TVs are in a household. For this example the range I am working on is 1 to 11. I could use:
randomValue = Int(Rnd * 11) + 1
But I want the random results to be based on a percentage. For this example (see below), when I create my data, I want 4% of housholds to have 1 TV, 8% to have 2 TVs, 12% have 3% and so on...
14%
28%
312%
420%
523%
613%
711%
83%
93%
102%
111%
How can I generate this? There is probably already an example here for this, but I do not know what it is called to be able to search for it.
Thank you.
Random Number Generating Question??
Hullo again...
I've a question for the guru's (or anyone who can answer it)
I'm trying to create some random numbers for an array of objects.
I can't seem to figure out how to create random numbers for all of my objects where no two are alike.
Example: I have 10 objects, A B C D E ....etc... I need to be able to create a random number between 1 - 10 for each object not using the same number twice.
A = needs to be a random number
B = needs to be a random number and not = to A
C = needs to be a random number and not = to A and B
.....etc....
Do I need to use loop statements?
An example would be GOLD!
Thanks!
Generating Random Numbers For The Lottery
Someone was asking recently abount generating random numbers for the lottery. Here's the code...
I've commented it as best I can, but if you've any questions, let me know...
_____________________________________________________________
Option Explicit
'1. Select 6 random numbers from 42
'2. Sort them
'3. if any numbers are the same, select a random number to replace them
'4. repeat steps 2 & 3 until all numbers are different
'When debugging this code, set ArraySize to a large number (say 25), so
'you'll get lots of duplicates and so testing will be easier.
Sub Main()
Dim i As Integer
Dim NumberString As String
Dim temp1 As Integer
Dim Sorted As Boolean
Const ArraySize = 6 'Choose this amount of numbers from TotalNumbers...
Const TotalNumbers = 42 'Choose from these numbers...
Dim arrRandom(1 To ArraySize) As Integer
Randomize
For i = 1 To ArraySize
arrRandom(i) = Int(Rnd * TotalNumbers) + 1
Next i
Sorted = False
'The nested while loop is not wholly necessary for the program to work.
'You can take it out. The reason I've included it is for efficiency -
'when the nested while finishes, the numbers will be sorted, ready for
'comparison. If the nested while loop were excluded, the test for
'duplicates would not be comparing values from a sorted array and would
'therefore be pointless.
'But if you can come up with a nicer way of doing it...
While Sorted = False
'Sort the array in ascending order
'The logic here assumes that if one swap is made, the array is not
'sorted, so it goes back to the top and sorts again. When no swaps
'are made, the Sorted variable will be true and the loop will end.
While Sorted = False
Sorted = True
For i = 1 To UBound(arrRandom) - 1
If arrRandom(i) > arrRandom(i + 1) Then
temp1 = arrRandom(i)
arrRandom(i) = arrRandom(i + 1)
arrRandom(i + 1) = temp1
Sorted = False
End If
Next i
Wend
'Check for duplicates - if there are any, replace them with another
'random number. When the for loop finishes, we need to sort again,
'at which point we go back to the top of the while loop.
For i = 1 To UBound(arrRandom) - 1
If arrRandom(i) = arrRandom(i + 1) Then
arrRandom(i) = Int(Rnd * 42) + 1
Sorted = False
End If
Next i
Wend
'Output the chosen numbers via a message box
NumberString = arrRandom(1)
For i = 2 To UBound(arrRandom)
NumberString = NumberString & ", " & arrRandom(i)
Next i
MsgBox NumberString
End Sub
Excluding Numbers From Random Generating
Good day!
I develop a random number generator. I need to generate, for example, numbers from 1 to 90. How can I exclude from generating such numbers, like, for example, 89,76,2 and 7. How can I do that? What I was doing is adding each new generated number to a collection of restricted numbers, so if such a number appears, it generates another one and so on until all numbers are generated. But the presented algorithm is not the optimal one I think. Any ideas on how to implement the functionality?
Thanks!
|