WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Seed Random()Function

Keywords:   seed random()

Question:

The random() function seems to return the same value each time my winbatch program is called. It is always called from the windows Startup directory when the computer reboots.

Is there any way to seed random() so it doesn't return the same value every time it is first called?

Answer:

Try adding a:
	TimeDelay(1)
before the Random.

Random is "auto-seeded" with the number of clock ticks since windows has started.

If it is in your startup group, it may be getting the same number of clock ticks on each invocation, resulting in the same random number.

TimeDelay(1) will wait till the next clock second has passed - which will be a varying number of real clock ticks, and this ought to result in better random numbers.


Question:

How would/can I use the Random function to specify a range? i.e. I'm trying to generate a Random number in the range of 10-99 (2-digits). Using:
	RDM=random(99)
RDM results in a range from 0-99. (one and two digits). Maybe I'm using the wrong function?

Answer:

	RDM=10+Random(89)

Unique Temporary FileNames:

Question:

Is there a function to give you a UNIQUE Temporary File Name? I remember that Windows itself can do this for programs who need temp files and don't want to 'hard-code' their names. If there isn't a function or way to do it within WinBatch, would you consider adding one?

Answer:

See the function FileCreateTemp, it can create a temporary file. This function creates a 0-byte file with a unique name, in the directory designated for temporary files (as specified by the "TMP" or "TEMP" environment variable).

The temporary file name will have the form:

preXXXX.tmp

Where "pre" is the prefix specified by the user, and "XXXX" is a 4-character hexadecimal string generated to guarantee a unique file name. The file name will have an extension of ".tmp".


Selecting Randomly from a List

Question:

Can we randomly pick a number between a set number ? eg. between 49 to 57. Is it possible to randomly pick an item from the list too ?

Answer:

for i=1 to 20
   a=49+Random(8)
   TimeDelay(0.25)
   Message("",a)
next

Question (cont'd):

I am sorry that I did not clarify what I need. My point is the randomly pick a given list of colors eg. list=Strcat("Red","Blue","Green",Yellow",Brown")

Answer:

Put the colors in a TAB delimited list, get a random number, then pick that item from list.
list="red green brown black purple yellow magenta"
count=ItemCount(list, " ")

while 1
        rand=Random(count -1 ) + 1
        color=ItemExtract(rand,list, " ")
        Pause(rand,color)
endwhile

Article ID:   W13099
Filename:   Random() Function.txt
File Created: 2001:01:12:10:51:36
Last Updated: 2001:01:12:10:51:36