DrOtosclerosi Posted November 11, 2019 Share Posted November 11, 2019 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 More sharing options...
RTFC Posted November 11, 2019 Share Posted November 11, 2019 Please study the help file before you post My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
DrOtosclerosi Posted November 11, 2019 Author Share Posted November 11, 2019 Just now, RTFC said: Please study the help file before you post Thanks, I've missed that part even after googling. Link to comment Share on other sites More sharing options...
RTFC Posted November 11, 2019 Share Posted November 11, 2019 No need to google. In the AutoIt Help file's topic Random, under heading "Related," there's a direct link to SRandom(). My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
DrOtosclerosi Posted November 11, 2019 Author Share Posted November 11, 2019 Ok thank you again I must have missed that Link to comment Share on other sites More sharing options...
BugFix Posted November 11, 2019 Share Posted November 11, 2019 If it must unique you can use _WinAPI_CreateGUID() . Best Regards BugFix Link to comment Share on other sites More sharing options...
DrOtosclerosi Posted November 11, 2019 Author Share Posted November 11, 2019 26 minutes ago, BugFix said: If it must unique you can use _WinAPI_CreateGUID() .I don't know GUID very much, is GUID a random number? Link to comment Share on other sites More sharing options...
TheXman Posted November 11, 2019 Share Posted November 11, 2019 (edited) 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 November 11, 2019 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
iamtheky Posted November 11, 2019 Share Posted November 11, 2019 Musashi 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
DrOtosclerosi Posted November 12, 2019 Author Share Posted November 12, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now