Re: random filenames
- Posted by Jason Gade <jaygade at yahoo.com> Mar 19, 2007
- 520 views
jacques deschĂȘnes wrote: > > Hi Hayden, > the reason for unprintable character is that rand(125)+1 generate values > between > 1 and 126 and codes below 32 are unprintable > replace rand(125)+1 by rand(93) + 32 > but beware that some characters like '\' are forbidden in windows filenames > so a better choice could be rand(26)+'a' (limit to lower case letters) > But all this is not 100% collision proof. > > regards, > Jacques D. > > Hayden McKay wrote: > > > > Doe's anyone know how to implement a small runtime cross-platform random > > filename > > generator? > > > > I'm currently just adding the '~' char to the filename > > ie: filename = '~' & filename > > > > I've written a small template but it will return unprintable chars; > > > > }}} <eucode> > > function TempFile(sequence mode) > > sequence s > > > > s = repeat(-1, 8) > > for j = 1 to 8 do -- DOS filename compatablity > > s[j] = rand(125 + 1) > > end for > > > > s &= ".TMP" > > > > return OpenFile(s, mode) > > > > end function > > </eucode> {{{ > > > > Maybee a 'set_rand()' might fix? Here's an (untested) idea:
constant CHARLIST = "abcdefghijklmnopqrstuvwxyz1234567890" function TempFile(sequence mode) sequence s s = repeat(-1, 8) integer charcount charcount = length(CHARLIST) for j = 1 to 8 do -- DOS filename compatibility s[j] = CHARLIST[rand(charcount)] end for s &= ".TMP" return OpenFile(s, mode) end function
I just noticed that Christian Cuvier's upload is showing up, I haven't looked at it yet. -- "Any programming problem can be solved by adding a level of indirection." --anonymous "Any performance problem can be solved by removing a level of indirection." --M. Haertel "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.