1. random filenames

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;

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


Maybee a 'set_rand()' might fix?

new topic     » topic index » view message » categorize

2. Re: random filenames

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?


You probably can add a random touch to my unique.e in the archive.

CChris

new topic     » goto parent     » topic index » view message » categorize

3. Re: random filenames

CChris wrote:
> You probably can add a random touch to my unique.e in the archive.
> 
> CChris

Where is that? I couldn't find it by searching for "unique" in the archive, nor
by trying "unique.e" or "unique.zip" directly.

--
"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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: random filenames

Jason Gade wrote:
> 
> CChris wrote:
> > You probably can add a random touch to my unique.e in the archive.
> > 
> > CChris
> 
> Where is that? I couldn't find it by searching for "unique" in the archive,
> nor by trying "unique.e" or "unique.zip" directly.
> 
> --
> "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.

ooops!! It's a project I had started, but didn't have time/patience to
 polish. I just submitted it. Cannot guarantee it works always well,
 for lack of sufficient testing.

CChris

new topic     » goto parent     » topic index » view message » categorize

5. Re: random filenames

Thanks, Christian. I'll check it out.

I've seen the solution to this in *nix shell scripts -- basically the script
grabs the PID (process ID) of it's own process and uses that (maybe with a random
or iterative number added on).

I don't know how easy it is to get the PID under Windows though. I found some
tools online to do so but I didn't see if there was anything already "built-in"
to Windows to do it (like with system()), without calling into the OS itself.

--
"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.

new topic     » goto parent     » topic index » view message » categorize

6. Re: random filenames

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?

new topic     » goto parent     » topic index » view message » categorize

7. Re: random filenames

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.

new topic     » goto parent     » topic index » view message » categorize

8. Re: random filenames

After looking at Christian Cuvier's entry, he does something similar but much
more. He checks to make sure the random name doesn't match an existing filename
in the directory, and a lot more. It's worth checking out.

--
"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.

new topic     » goto parent     » topic index » view message » categorize

9. Re: random filenames

Thanks to all for your input guys. I've know adapted Jason's technique but
instead of the byte array i'm useing;
if ch >= 'A' and ch <= 'Z'
or ch >= 'a' and ch <= 'z'
-- etc...

then check filename with 'dir()' and recurce if need to.

nb. the code had to remain small and efficiant, it's later going to be
translated to assembly by someone else.

new topic     » goto parent     » topic index » view message » categorize

10. Re: random filenames

Hayden McKay wrote:
> 
> Thanks to all for your input guys. I've know adapted Jason's technique but
> instead
> of the byte array i'm useing;
> }}}
<eucode>
> if ch >= 'A' and ch <= 'Z'
> or ch >= 'a' and ch <= 'z'
> -- etc...
> </eucode>
{{{

> then check filename with 'dir()' and recurce if need to.
> 
> nb. the code had to remain small and efficiant, it's later going to be
> translated
> to assembly by someone else.

Can you share the code?

I think a look-up table (which was my technique) would be more efficient even in
assembler than a decision table. But I'm not an experienced assembly programmer.

--
"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.

new topic     » goto parent     » topic index » view message » categorize

11. Re: random filenames

Jason Gade wrote:
> 
> Hayden McKay wrote:
> > 
> > Thanks to all for your input guys. I've know adapted Jason's technique but
> > instead
> > of the byte array i'm useing;
> > }}}
<eucode>
> > if ch >= 'A' and ch <= 'Z'
> > or ch >= 'a' and ch <= 'z'
> > -- etc...
> > </eucode>
{{{

> > then check filename with 'dir()' and recurce if need to.
> > 
> > nb. the code had to remain small and efficiant, it's later going to be
> > translated
> > to assembly by someone else.
> 
> Can you share the code?
> 
> I think a look-up table (which was my technique) would be more efficient even
> in assembler than a decision table. But I'm not an experienced assembly
> programmer.
> 
> --
> "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.

It is. Obviously you are using more memory to pay for an increase in speed.
This is even more true on pipelined CPUs, where bad branch prediction has a
 sgnificant cost.

CChris

new topic     » goto parent     » topic index » view message » categorize

12. Re: random filenames

Jason Gade wrote:
> 
> Can you share the code?
> 
> I think a look-up table (which was my technique) would be more efficient even
> in assembler than a decision table. But I'm not an experienced assembly
> programmer.
> 

not optimized and thougherly tested yet but here it is. In euphoria the lookup
table you used would be ~256 bytes and alot faster then my one shown here. In
assembler the lookup table would be ~64 bytes but the decision method could
compile to 24 -> 32 bytes ( not incudeing rand func etc... ).

include file.e

function TempFile(sequence m)
    integer i
    sequence s

    s = {}
    while length(s) < 8 do
        i = rand('z')
        if (i >= '0' and i <= '9')
        or (i >= 'A' and i <= 'Z')
        or (i >= 'a' and i <= 'z')
        then
            s &= i
        end if
    end while

    s &= ".tmp"

    if sequence(dir(s)) then
        return TempFile(m)
    else
        return OpenFile(s, m)
    end if

end function


new topic     » goto parent     » topic index » view message » categorize

13. Re: random filenames

Yeah, I always forget whether to specify "size" or "speed" when using the word
"efficiency". I usually think speed to myself but sometimes the tradeoff becomes
rather obvious.

Is this for an embedded project or something?

--
"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.

new topic     » goto parent     » topic index » view message » categorize

14. Re: random filenames

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?

I tested your method out and found that


rand(26) + 'a' needs to be rand(25) + 'a'

to avoid '{' char

how many programmers does it take to change a light bulb? none it's a hardware problem! }}}

new topic     » goto parent     » topic index » view message » categorize

15. Re: random filenames

Jason Gade wrote:
> 
> Thanks, Christian. I'll check it out.
> 
> I've seen the solution to this in *nix shell scripts -- basically the script
> grabs the PID (process ID) of it's own process and uses that (maybe with a
> random
> or iterative number added on).
> 
> I don't know how easy it is to get the PID under Windows though. I found some
> tools online to do so but I didn't see if there was anything already
> "built-in"
> to Windows to do it (like with system()), without calling into the OS itself.
> 
> --
> "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.

kernel32.dll exports a GetCurrentProcessId() function that just returns that:
include dll.e
constant kernel32=open_dll("kernel32.dll"),
getPID=define_c_func(kernel32,"GetCurrentProcessId",{},C_LONG)
-- if using win32lib, kernel32 is already known, and it may be better to use
-- registerw32Function() and w32Func() iinstead of the bare Eu builtins.

function myPID() 
return c_func(getPID,{})
end function


CChris

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu