How random is random?

# Thread: How random is random?

1. Senior Member
Join Date
Dec 1969
Posts
105

## How random is random?

I have 4 pages:<BR>- page 1: Get some textinput from user and ask if (s)he wants to add a jpg image to it (in this case it&#039;s &#039;yes&#039;)<BR>- page 2: show upload form and keep state using hidden form fields<BR>- page 3: generate a random string and process uploaded image saved as &#060;randomstring&#062;.jpg<BR>- page 4: preview and button to save it all or cancel. After pressing save, a record is added to the (mysql) database with all the posted data and a field &#039;image&#039; with the name as generated in page 3.<BR><BR>Now I have a user that&#039;s doing the above for 1.500 times and was complaining that some pictures got overwritten. Looking at the records I saw that indeed the randomstring names were identical for those records. How is this possible and how can I fix this?<BR><BR>I use the following:<BR><BR><BR>&#039;generate a random string to create unique file name<BR>randomize<BR>SD_count = 1<BR>DO<BR>&#039;get random number b/w 48 & 122 (0 - Z)<BR>intRnd = int(75 * rnd + 48)<BR>&#039;limit string to 0-9, a-z, A-Z<BR>if (intRnd &#060; 57 OR intRnd &#062; 65) AND (intRnd &#060; 90 OR intRnd &#062; 97) then<BR> listRnd = listRnd & chr(intRnd)<BR> SD_count = SD_count + 1<BR>END IF<BR>&#039;make random string 15 chars gives 768,909,704,948,766,668,552,634,368 possible combinations on UNIX (case sensitive) or 221,073,919,720,733,357,899,776 combinations under Windows - should be enough )<BR>LOOP UNTIL SD_count = 16<BR>&#039;make it a little more readable<BR>strRandom = "{" & mid(listRnd,1,5) & "-" & mid(listRnd,6,5) & "-" & mid(listRnd,11,5) & "}-"<BR><BR>filename = strRandom & ".jpg"<BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: How random is random?

Include a timestamp (down to seconds) in your random number. That doesn&#039;t increase the number of combinations (in fact, it reduces it), but it means that there&#039;s less likelihood of things happening at the same time.<BR><BR>After all, if you stick year/month/day/hour/minute/second into the name, that second is then passed - it&#039;s never (ever) going to be repeated. The randomness you need to introduce is only a factor of the number of people who&#039;ll be using the system at that second.<BR><BR>Craig.

3. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## For my generated file names..

.. I rarely use anything random:<BR><BR>If you need it to be really unique, do something like:<BR>YYYYMMDDHHMiSS_TIMER_Session_ID<BR><BR>It &#039;ll be long, but it&#039;ll be unique.<BR><BR>Alternatively - are you saving something to the DB? Just use an IDENTITY field in the database for your filename?

4. Senior Member
Join Date
Dec 1969
Posts
105

## RE: For my generated file names..

Thanks for your reply&#039;s. I think using the time is indeed just what I need to get the filenames unique. Besides, every user has it&#039;s own folder so even when there are a lot of users at the same time which could give the same filename, it won&#039;t matter.<BR><BR>Thanks again!<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## To answer question: Not very

There are only (I believe) 32767 (or maybe 65535) random sequences, because the random number "seed" is an integer, if I remember correctly.<BR><BR>Even if it&#039;s a long integer, there would only be 2 billion random sequences. And while it would take a coincidence to hit the same sequence twice, it&#039;s by no means impossible.<BR><BR>If you want a really random number, ask for a GUID (though you&#039;ll have to use a component to do so, I believe).<BR><BR>But I agree with the others, for your purposes the TOD and SessionID stamp is much better.<BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•