I am creating an app whose main purpose is to collect data for experiments, which it uploads to a database at the end of each experiment. I want to assign a unique identifier each time the experiment is run (either opening the app, or restarting the experiment).
I cannot use an identifier that is unique to the device only, since there will be multiple experiments performed on each device.
I cannot use an identifier based on the time of the experiment (or some other form of sequential assignment), since it is possible for multiple devices to submit their data at the same time.
At the moment, my solution is to assign a random number each time an experiment is run, which is checked against a database and recalculated if it exists. This isn't perfect since it is possible, but rare, that two experiments will be run at the same time and assigned (by chance) the same identifier.
How can I assign a unique identifier each time the experiment is run?