How to generate random string that contains alphanumeric characters with specified number of length of the string. Example are ckd938, jfp393 etc if we need the length of the string to be 6.
How to generate random string that contains alphanumeric characters with specified number of length of the string. Example are ckd938, jfp393 etc if we need the length of the string to be 6.
Hi @Preet, was Carter Walch's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.
If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.
Hi @Preet ,
You could do something like this to make a six digit alphanumeric string randomly. You can adjust the numLetters and numNumbers variables depending on the length of the string you want. Example output below.
string letters = "abcdefghijklmnopqrstuvwxyz"; int numLetters = 3; int numNumbers = 3; string result = ""; //append the letters for (int i = 0; i < numLetters; i++) { int randomIndex = duniform(1,26); string newChar = letters.charAt(randomIndex); result = result + newChar; //print(newChar); } //append the numbers for (int i = 0; i < numNumbers; i++) { int randomNum = duniform(0,9); string newChar = string.fromNum(randomNum); result = result + newChar; //print(newChar); } print(result);
Hope that helps!
An alternative:
string result; Array mix=[3,3]; // 3 letters, 3 numbers for (int n=mix[1];n>0;n--) result+=strascii(duniform(97,122)); // for capitals use 64-90 result+=string.fromNum(duniform(1,Math.pow(10,mix[2])-1),0).padStart(3,"0"); return result;
For both approaches you should add a stream to duniform. If this is to become a user command, pass one in as parameter.
15 People are following this question.
FlexSim can help you understand and improve any system or process. Transform your existing data into accurate predictions.
FlexSim is a fully 3D simulation software environment. FlexSim can be used to simulate any process in any industry.
FlexSim®, FlexSim Healthcare™, Problem Solved.®, the FlexSim logo, the FlexSim X-mark, and the FlexSim Healthcare logo with stylized Caduceus mark are trademarks of FlexSim Software Products, Inc. All rights reserved.
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © Autodesk Inc. All rights reserved