I've never tried writing Powershell so I'm clueless as to the syntax.
I have a line of code that generates a random 8 digit integer. What I need is some more code to test whether this integer contains any duplicate adjacent numbers, such as 92257032 or any sequential ascending adjacent numbers, such as 73978025 and, if either of these conditions are true then go back to the line that generates the random number and do it again.
Is this an easy bit of code for anyone to write please ?
Ta Muchly !
Lack of planning on your part does not constitute an emergency on mine.
Chips are "Plant-based" no matter how you cook them
Donald Trump needs kicking out of a helicopter
I'm personally responsible for all global warming
Comments
The basic approach would be:
Loop i from 0 to length of the number - 1
At every iteration check the digit at i and at i+1, if they are the same or consecutive then go back to the start
To get the individual numbers you can do something like
digit = number/(10^i) % 10.
regular expression adjacent duplicate characters
You should find a jsfiddle example. Then it is easy to convert a number to string and vice versa finally using a regex to check in powershell is easy.
*Googles pre-prepared pattern*
”Yep, one day I’m going to properly learn Regex.”
Basically, it's going to be;
Generate Random Number
Convert integer to string
for character n in string
if (value of character n = value of character n+1) OR (value of character n - value of character n+1 = -1) then go to Generate Random Number
That's it.
I just don't know any powershell syntax !
Chips are "Plant-based" no matter how you cook them
Donald Trump needs kicking out of a helicopter
I'm personally responsible for all global warming
My feedback thread is here.
But I would recommend reading about Powershell Functions: https://4sysops.com/archives/the-powershell-function-parameters-data-types-return-values/ Rather than generate all the string then check for duplicates on the previous, should be less attempts to get a unique number? Should be fine.
JM build | Pedalboard plans
Chips are "Plant-based" no matter how you cook them
Donald Trump needs kicking out of a helicopter
I'm personally responsible for all global warming
The first of these will be the first digit of your number. Then add this first digit to the second random number (modulo 10) to get a digit that is guaranteed to be different and non-sequential. This will be your second digit. Then add this to the third random number to get the next digit, and so on.
Keep on like that through all 7 following digits and you’ll have an acceptable number. This involves no checking, only some additions, so may be quicker.
Apologies for not reading the whole post, a bad habit of mine which is getting worse as I get older - frighteningly so.
OK try this which only checks for ascending digits with a difference of 1. If you wanted a descending check then absolute value would be needed before the check -ne 1
Thanks again. Very much appreciated !!
Chips are "Plant-based" no matter how you cook them
Donald Trump needs kicking out of a helicopter
I'm personally responsible for all global warming
Chips are "Plant-based" no matter how you cook them
Donald Trump needs kicking out of a helicopter
I'm personally responsible for all global warming