Stringi_rand_strings - repeating strings from time to time

,

Hello everyone,

I hope you're doing well.

Newbie on the Shiny + R solutions with a question about rand_strings.

I have a portion of code on with the stringi_rand_strings is used to generate a random string to create a sort of unique identifier and then we write a row into a table in a database. The code was inherited from someone else that used to run it in a local server with Shiny+R. We currently have it hosted in Basic Shiny plan and while it relatively does what we need, from time to time I use to get the exact string so we end up with more than 1 record for the particular randomized string while each has its very own fields. The mini app is used by 5 users total and while they connect to the app using their own user/password and can generate as many records as they like the also get the same string (different time of the day, logged in at different shifts/hours, selecting different options).

The line of code is very simple using:

uniquecode = stringi::stri_rand_strings(1, 12)

Which creates 1 string of 12 characters. A few examples of the strings I get are:

mQmTbCUsXcTa
NkjOC8wOmOeo
8Ql6j6VM9TXr

Which is pretty much what we're looking for this small app, except for this not so often and very odd occasions where the same string is get. When the app is not used, it goes into standby mode after 15min as the Shiny dashboard defaults which is also fine for our purpose.

Could this behavior be caused by the Shiny container where it is running? could it be caused by the standby / back on process that affects the way rand_strings generates the string? or just pure bad programming skills?. Based on the user's input it didn't happen when in the local shiny server and seem to have started when we hosted in Shinyapps.

Thank you very much for your time and help.

Hi @CrtxIT, welcome to the community!

I don't think it's due to bad programming skills, just that the 12-length string isn't random enough, so that even though it's improbable, it's still possible to see duplicates.

Does the string have to be only 12 characters long? If not, the uuid package should generate a sufficiently random string that isn't influenced by R seed values or anything else.

Best,
Randy

Are you generating just one string in each run, or a sequence of strings? If you generate multiple strings at one time and a previously encountered string repeats, is the next string also the same as the next string from the previous encounter? That would suggest a recurrence of the random number seed.

1 Like

Assuming the pseudorandom number generator is pretty random, the probability of any one 12 character string repeating is about 3x10^-22. So while I agree that a repeat is possible, it's extremely unlikely (unless you are generating a huge number of strings), and the likelihood of seeing repeats more than once is even lower. The catch is that this is based on runs not repeating the same initial seed.

Hi @randyzwitch,

we sticked to 12 because it was "working" before :smiley: but we can definitely modify it. I am going to try the uuid package, curious about it and if it brings in an improvement, even better!

Hey @prubin,

Thanks for your reply. It is 1 string per run, the mini app is a quote generator for a small number of customers a day (30 quotes a day tops, generated by any of the 5 app users). I am going to try Randy's suggestion but also will review variables that get this string to confirm it is being reset / cleaned up right after the end of the workflow. String changes almost every time which makes me think variables are cleared but I'd rather confirm.