none
Random string function returning the same strings every time RRS feed

  • Question

  • Hello.

    I have a simple question regarding the usage of "rnd". I have attempted to implement it to create random filenames. However on each run it generates the same "random" strings.

    function getrandomstring(ofLength,mode)
      	retval = ""
      	For i = 1 To ofLength
    		if mode = 0 then
    			retval = retval & Chr(Int(89*Rnd+33))
    		elseif mode = 1 then
    			If Int(2*rnd) = 0 then
    				retval = retval & Chr(Int(26*Rnd+65))
    			else	retval = retval & Chr(Int(26*Rnd+97))
    			end if
    		end if
      	Next
    	getrandomstring = retval
    end function
    
    for x=0 to 9
    	wscript.echo getrandomstring(9,1)
    next

    Everytime I run this script, it spits out the same exact 9 "random" strings?

    Please help


    
    
    • Edited by KCSteele Thursday, March 19, 2015 4:39 PM edited for legibility
    Thursday, March 19, 2015 4:35 PM

Answers

  • I have a simple question regarding the usage of "rnd". I have attempted to implement it to create random filenames. However on each run it generates the same "random" strings.

    About the "random" component of your question: Here is the help file text taken from script56.chm (downloadable from the Microsoft site).

    For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

    Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.


    • Edited by Frederik Long Thursday, March 19, 2015 4:56 PM
    • Marked as answer by KCSteele Thursday, March 19, 2015 5:17 PM
    Thursday, March 19, 2015 4:55 PM

All replies

  • No need to reinvent the wheel. Just use the GetTempName method of the FileSystemObject object.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 19, 2015 4:46 PM
    Moderator
  • Thanks Bill

    I am not just using it for creating random filenames however. I also am using it to populate each of the files with random characters. (This is the reason for mode "0" as it is supposed to include random characters instead of just upper/lowercase alpha).  

    set file=createobject("scripting.filesystemobject")
    for x=0 to 9
    	filename = getrandomstring(9,1) & ".txt"
    	wscript.echo filename
    	set writer = file.opentextfile(filename,2,true)
    	for z=0 to 50
    		writer.writeline(getrandomstring(127,0))
    	next
    	writer.close
    next

    so there is the code for doing that. Subsequently, the data written to each file is also the same each time. Which I cannot figure out why??

    Thursday, March 19, 2015 4:54 PM
  • I have a simple question regarding the usage of "rnd". I have attempted to implement it to create random filenames. However on each run it generates the same "random" strings.

    About the "random" component of your question: Here is the help file text taken from script56.chm (downloadable from the Microsoft site).

    For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

    Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.


    • Edited by Frederik Long Thursday, March 19, 2015 4:56 PM
    • Marked as answer by KCSteele Thursday, March 19, 2015 5:17 PM
    Thursday, March 19, 2015 4:55 PM
  • Thanks dude!
    Thursday, March 19, 2015 5:17 PM