I want to make a small random message generator. The messages can either me part of the code or better still, seperate in a text file.
Where to start?
ps I have searched the forums, but most posts seem to about random numbers.
Thanks mojo
ruti_gl
09-12-2000, 02:36 AM
Who prevents you from creating message string array, which
elements according to their indexes you will process like
random numbers?
Good Luck!
Ruth Glushkin
usetheforce2
09-12-2000, 10:43 AM
an easy way to do that, would be as the previous post'r suggested, create an array(depending on how many messages, and how long you want them) then just call the different messages randomly
you could have a sql txt file and line input each line into different elements of the array. such as
in note pad just open a file and type somelines into it(as many as you want, hit return at the end of each linek)
save the file as File.txt in the app directory, then put this code into your form load
------------form load---------------
dim RandomString() as String
dim Counter as integer
open app.path + "File.txt" for input as #1
do while not eof(1)
redim preserve RandomString(counter)
line input #1, randomstring(counter)
counter = counter + 1
loop
close #1
-------------------------------------------------------
now that you have random messages in you array, you can call them
place this code into a command button
----------command1_click------------------
randomize
dim RandomNumber as integer
RandomNumber = 1 to (counter - 1) * rnd + 1
label1 = randomstring(randomnumber)
----------------------------------------------
i'm at school right now, and i haven't tested it it should work though (my appologies if it doesn't) feel free to email me if you have any question.
ps
i;m sure if i miss anything one of the other fine gentlemen
will correct my mistake.
Regards
Regan DeDiana
All looks ok to me, the only thing that I would add is that when you use 'Randomize' it creates a random number against a predefined seed. Because of this every time you run the code you would get the same sequence of returns, an idea may be to 'Randomize Timer' instead. This way you will get different results each time the code is run.
Hope it help!
Live2Give
For some reason it gives me a Syntax error at the line
RandomNumber = 1 to (counter - 1) * rnd + 1
Any ideas?
ruti_gl
09-14-2000, 03:39 AM
To which value your counter is equal?
If 0, it may be a problem.
Another problem could be in type,
if it is not number value, so how
have you declared it?
Or, maybe, you forgotten to declare it
at all?
Send the whole subroutine,
because it is not good to suggest.
With Full Respect,
Ruth Glushkin
BillSoo
09-14-2000, 01:03 PM
I think whoever wrote this meant it as pseudocode rather than actual code.
Try:
RandomNumber = cint(counter * rnd())
where:
counter is the number of entries in your array and your array is zero based. This line will return a random value of RandomNumber that is between counter-1 and 0. Incidentally, the counter variable must be declared at the module level rather than procedural level.
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
usetheforce2
09-14-2000, 03:26 PM
sorry about the errors, did this at school just copy paste this into you app.
add one label1
add a command1
--------cut here---------------
Option Explicit
Dim RandomString() As String
Dim Counter As Integer
Dim RandomNumber As Integer
Private Sub Command1_Click()
Randomize
RandomNumber = (Counter - 1) * Rnd + 1
Label1 = RandomString(RandomNumber)
End Sub
Private Sub Form_Load()
Open App.Path + "File.txt" For Input As #1
Do While Not EOF(1)
Counter = Counter + 1
ReDim Preserve RandomString(Counter)
Line Input #1, RandomString(Counter)
Loop
Close #1
End Sub
------------------cut here-----------
just make sure to add a file in the directory call file.txt
enter some lines of whatever you want hit enter at the end of each line
that's it
sorry agian, it hard to get good help these days, "isn't it"
:)
take care:
Regan DeDiana
Thanks
I'll try it out when I get home tonight (if I can tear myself away from the Olympic Opening Ceremony, sarcasm intended).
Will let you know