% :- defines( swi(_), [mktemp/2] ). mktemp( Base, File) :- atom_concat( RealBase, 'XXXXXX' ,Base ), mktemp_1( RealBase, File ). mktemp_1( RealBase, File ) :- Rgt is 0'z + 1, Excl = [[0'9,0'A],[0'Z,0'a]], randoms_in_range_with_exclusions( 6, [0'0,Rgt], Excl, Rands ), atom_codes( Psfx, Rands ), atom_concat( RealBase, Psfx, PrvFile ), ( exists_file(PrvFile) -> mktemp_1( RealBase, File ) ; File = PrvFile ). randoms_in_range_with_exclusions( 0, _Range, _Excl, Rands ) :- !, Rands = []. randoms_in_range_with_exclusions( N, Range, Excl, Rands ) :- Range = [From,To], % random( From, To, Rnd ), PrvRnd is random( To - From ), Rnd is PrvRnd + From, randoms_in_range_with_exclusions_1( Rnd, N, Range, Excl, Rands ). randoms_in_range_with_exclusions_1( Rnd, N, Range, Excl, Rands ) :- ranges_include_number_excluding_marks( Excl, Rnd ), !, randoms_in_range_with_exclusions( N, Range, Excl, Rands ). randoms_in_range_with_exclusions_1( Rnd, N, Range, Excl, Rands ) :- NxN is N - 1, Rands = [Rnd|TRands], randoms_in_range_with_exclusions( NxN, Range, Excl, TRands ). ranges_include_number_excluding_marks( [[Min,Max]|_T], Num ) :- Num > Min, Num < Max, !. ranges_include_number_excluding_marks( [_H|T], Num ) :- ranges_include_number_excluding_marks( T, Num ).