EDIT: I've re-done this post yet again...
NOTE: Works for both DBC and DBPro!
Let's say you need to get a random integer from 5 to 20.
Normally in DB you would need to do something like this:
However if you need to do that sort code in several places throughout your program,
you may find it easier just to use my 'randInt' function:
function randInt(min, max)
rand = (rnd(max - min) + min)
endFunction rand
Here's the full source dump for that example:
print randInt(5, 20)
suspend for key
end
function randInt(min, max)
rand = (rnd(max - min) + min)
endFunction rand
NOTE: The randInt function returns only integer values.
Now that's nice, but what if you need to get a random number thats not in a certain range? Well, then you use my 'randStringFromFile' function. This function opens up an file and returns a line in the file, by random. Now this function returns a string value, so the converting to either an interger or float, etc is left up to you.
The 'randStringFromFile' function:
function randStringFromFile(file$, totalValues)
randLine = (rnd(totalValues - 1) + 1)
open to read 32, file$
for lineNum = 1 to randLine
read string 32, string$
next lineNum
close file 32
endFunction string$
Now in order to use this function you need to tell it first what file to look in and then how many lines are in that file.
To find how many actual lines are in that file you use my 'totalLinesInFile' function:
function totalLinesInFile(file$)
open to read 32, file$
while file end(32) = 0
inc lineTotal
read string 32, string$
endWhile
close file 32
endFunction lineTotal
NOTE: This function simply opens up a file and returns the total amount of lines in that file as an integer value.
Now let's put these functions to use! Let's say you want to pick one of the following values by random: 10, 20, 35, 50.1, 8999.125. Now neither
the DB rnd() command nor my 'randInt' function can do this, so this is my way of accomplishing this task. First put all the values in a file, I'll use "randNumbers.txt".
Like this:
10
20
35
50.1
50.1
50.1
8999.125
NOTE: since the 50.1 value is listed more than once it will be more likely to be picked than all the other values.
Now that we have all our values in the "randNumbers.txt" file, we can now figure out how many lines or numbers are actually in that file. So we use the 'totalLinesInFile' function.
Like this:
totalValues = totalLinesInFile("randNumbers.txt")
Now that we have how many lines are in the file stored in the 'totalLines' integer varible, we can now actually use the 'randStringFromFile' function.
Like this:
print randStringFromFile("randNumbers.txt", totalValues)
Here's the full source dump for that example:
totalValues = totalLinesInFile("randNumbers.txt")
print randStringFromFile("randNumbers.txt", totalValues)
suspend for key
end
function randStringFromFile(file$, totalValues)
randLine = (rnd(totalValues - 1) + 1)
open to read 32, file$
for lineNum = 1 to randLine
read string 32, string$
next lineNum
close file 32
endFunction string$
function totalLinesInFile(file$)
open to read 32, file$
while file end(32) = 0
inc lineTotal
read string 32, string$
endWhile
close file 32
endFunction lineTotal
NOTE: Make sure you have all your values in the file, and on a seperate line in the specified file and also on blank lines.
NOTE: To get a more randomized result, you can put this at the top of your code:
If you have any questions/comments, please let me know