Data360 Analyze

 View Only
  • 1.  How to have random number with 4 digit.

    Employee
    Posted 09-15-2021 21:17

    Hi Team,

    I need some guidance to create a code to populate 4 digit random number sequence for each row. I am aware of function code random(), It is currently populating 9 digit sequence. Please let me know if there is feasible way to do for 4 digits.

    Regards,

    Jagdev



  • 2.  RE: How to have random number with 4 digit.

    Employee
    Posted 09-16-2021 01:37

    You can use the randint() function of the Python random module in a Transform node to generate a random integer in the specified range (0 to 9999 in this case). Then, as numbers less than 1000 will have less than four digits (characters), the number is converted to a string and padded with leading '0' characters.

    See the attached example data flow.

     

    Attached files

    Generate Random 4 Digit String - 16 Sept 2021.lna

     



  • 3.  RE: How to have random number with 4 digit.

    Employee
    Posted 12-15-2021 13:41

    Hi Adrain,

    Thanks for the code. It is working fine.

    Is it possible to produce a random number which is not in consecutive number like 1234 or 4321.

    Regards,

    Jagdev

     



  • 4.  RE: How to have random number with 4 digit.

    Employee
    Posted 12-16-2021 09:17

    I know of no standard module function that would produce such a nearly-random number.

    I think you would have to put the function that created the random 4-digit string in a While loop and then test the result to see whether the characters were consecutive and if they were then re-generate the 4-digit string again. If a non-consecutive character was detected you could set the return value and break out of the While loop. You could test for consecutive numbers by examining the difference between the first & second character, the second & third, etc to see if the absolute difference was less than two