views:

797

answers:

5

I would like to create random folder names on my website to store images and their thumbnails, but instead of using the full version of a generated guid, i was thinking about using just part of it, maybe just the first 8 characters and possibly base64 encode it. i am worried about possible collisions though.

Can someone point me in the right direction as to whether or not it is a good enough idea? are there alternative solutions that can be used if i want to keep the name under a certain number of characters?

UPDATE: I am trying to stay away from path.GetRandomFileName , since it uses raw guid and it is not 12 chars long ...

+3  A: 

You can use the Path class in System.IO to, among other things, generate random file and folder names.

GWLlosa
+5  A: 

System.IO.Path.GetRandomFileName()

pyrochild
the function creates folder names with guid, which is what i am trying to avoid. How can i get a folder with a name like "dGhlIG1pbmQs" for example?
zaladane
no it doesn't, it returns a 8.3 name. 8 characters, period, 3 characters, all alphanumeric.
pyrochild
A: 

See this answer. Why do you want to limit the length to 12 characters?

Jamie Ide
I would like to use the folder name as part of the image thumb name, so if i could keep it not too long, it would be a great.
zaladane
+1  A: 

If you're using a database for your web site, then you can use an auto-incrementing number or a sequence to generate unique filenames for the images.

Using Oracle you would create a sequence as follows;

CREATE SEQUENCE sequence_name MINVALUE value MAXVALUE value START WITH value INCREMENT BY value CACHE value;

To get the next value in the sequence you use the nextval of the sequence. For example

CREATE SEQUENCE seq_user_id;

SELECT seq_user_id.NEXTVAL FROM dual;

INSERT INTO test_bed (user_id, class_type, room_location) VALUES (seq_user_id.NEXTVAL, 'Underwater Basketweaving', 'RM1205');

Dennis Ecclestone
how would you generate that sequence?
zaladane
+12  A: 

Why not just use something like this, and loop round until you can create the file without conflict?

 const string ValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

 static string GenerateName(Random random, int length)
 {
     char[] chars = new char[length];
     for (int i=0; i < length; i++)
     {
         chars[i] = ValidChars[random.Next(ValidChars.Length)];
     }
     return new string(chars);
 }

The reason for passing in the RNG is to avoid the typical problems of creating a new RNG in the method (duplicates when called in quick succession) while not using a static RNG (which isn't threadsafe).

An alternative would be to have a single static readonly RNG, and serialize calls within GenerateName by locking.

The main point is that somehow you generate a random name, and you just keep trying to create files with random names until you succeed, which is likely to happen very quickly (12 chars [A-Z 0-9] gives 4,738,381,338,321,616,896 possible combinations).

Jon Skeet
+1 for problems with RNG
Jeremy Cron
+1 using a modification of this for generating coupon codes for a storefront. I'd use a single RNG - but is locking really necessary? the Random class isn't threadsafe?
TheSoftwareJedi
No, Random isn't thread-safe. I seem to remember that if you use it from multiple threads you can end up in a bizarre situation with a seed of 0 which repeats forever :(
Jon Skeet
Documentation says it's not guaranteed threadsafe, and if you look at the implementation with Reflector, you can see how it doesn't have any locking, and InternalSample() could easily have problems if multiple threads were in there at once...
AakashM