Jump to content

Random generates two identical numbers in different time


Recommended Posts

Hi all!

In a software I'm developing and mantaining for my company I'm generating a random between 1 and 65535 (float included) to create a file that must be unique in each instance.

The file is generated when a work report is saved, and put in a folder with a reference in the work report xml file.

Long story short, last week happened twice that two work report xml files pointed to the same random file (reports created even in different days)!

By now I changed the naming of the random file with:

Random(1, 64, 1) & @MDAY & "." & @MON & "." & "." & @HOUR & "." & @MIN & "." & @SEC & "." & @MSEC

So I solved the problem but...how could this happen? Just bad luck or misuse of random()?

Is there any way to generate a seed?

This is the part of the code generating the random:

Global $MagRand = Random(1, 65535)

And seems very straightforward.

Link to comment
Share on other sites

No need to google. In the AutoIt Help file's topic Random, under heading "Related," there's a direct link to SRandom().

Link to comment
Share on other sites

6 hours ago, DrOtosclerosi said:

So I solved the problem but...how could this happen? Just bad luck or misuse of random()?

How could this happen?  It happened because you obviously do not understand what the word random means or, at least, the difference between random and unique.  Randomness has absolutely nothing to do with uniqueness.  A random number generator can generate the same number several times in a row and it could still be a randomly generated sequence.

The easiest way to have fixed your original code would have been to check if the randomly generated number (file name) exists, and if so, generate another random number until the generate number (file name) doesn't exist. 

The _TempFile() function generates a file name that is guaranteed not to exist yet.  Maybe taking a look at the code behind the function will help you understand how it is done.  Better yet, you can just use the _TempFile() function and be done with it.  You obviously do not have any special criteria for the file name itself if you were just using a number between 1 and 65535.  So it should meet your needs.

Edited by TheXman
Link to comment
Share on other sites

rng.PNG.5edf2887e83076a3a0974c56420c7f00.PNG

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

23 hours ago, TheXman said:

How could this happen?  It happened because you obviously do not understand what the word random means or, at least, the difference between random and unique.  Randomness has absolutely nothing to do with uniqueness.  A random number generator can generate the same number several times in a row and it could still be a randomly generated sequence.

The easiest way to have fixed your original code would have been to check if the randomly generated number (file name) exists, and if so, generate another random number until the generate number (file name) doesn't exist. 

The _TempFile() function generates a file name that is guaranteed not to exist yet.  Maybe taking a look at the code behind the function will help you understand how it is done.  Better yet, you can just use the _TempFile() function and be done with it.  You obviously do not have any special criteria for the file name itself if you were just using a number between 1 and 65535.  So it should meet your needs.

Thanks a lot! I'm looking at _TempFile() and seems to be perfect!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...