pizza Posted August 21, 2010 Share Posted August 21, 2010 Hey everyone, this is my first post here so if this is in the wrong section please forgive me. I need help with generating a random number that needs to be within some parameters. The number must be 7 digits long and all of the single digits must add up to a number divisible by 7. (i.e. 4519405, 4+5+1+9+4+0+5 = 28) If the first number could have a chance to be a zero then that would be a bonus. If there is any way of going about this please let me know! Thanks! Link to comment Share on other sites More sharing options...
4ggr35510n Posted August 21, 2010 Share Posted August 21, 2010 (edited) RandomModUse helpfile.Local $number_string = "0" Local $the_sum For $i = 1 to 5 $next_digit = Random(0,9,1) $number_string &= $next_digit $the_sum += $next_digit Next $number_string &= 7 - Mod($the_sum, 7) MsgBox(0,"",$number_string)Use HelpFile. Edited August 21, 2010 by 4ggr35510n Link to comment Share on other sites More sharing options...
pizza Posted August 21, 2010 Author Share Posted August 21, 2010 Random Mod Use helpfile. Local $number_string = "0" Local $the_sum For $i = 1 to 5 $next_digit = Random(0,9,1) $number_string &= $next_digit $the_sum += $next_digit Next $number_string &= 7 - Mod($the_sum, 7) MsgBox(0,"",$number_string) Use HelpFile. Thank you so much this is exactly what i needed. Link to comment Share on other sites More sharing options...
czardas Posted August 22, 2010 Share Posted August 22, 2010 (edited) After several attempts I have arrived at a working solution. My first attempt had several errors. 4ggr35510n's method seems simpler, but it also has some flaws. The first digit will always be zero, and the last digit will never be zero or greater than 7. So it isn't entirely random. Here is the corrected version. Apologies for the incorrect code I posted earlier. $digits = Random(7, 999999, 1) ; Create a random figure greater than 6, but not longer than 6 digits. $aiArray = StringSplit($digits,"",2) ; Separate the individual digits and store them in an array. $iTotal = 0 For $i = 0 To Ubound($aiArray) - 1 ; Add all the digits together. $iTotal += $aiArray[$i] Next $lastDigit = 7 - Mod($iTotal, 7) ; Determine what the seventh digit needs to be. If $lastDigit = 0 Then ; At Random, replace this particular zero with 7 (or leave it alone). $i = Random(0, 1, 1) If $i = 1 Then $lastDigit = 7 EndIf EndIf While StringLen($digits) < 6 ; Prefix zeros until the string is 6 digits in length. $digits = "0" & $digits WEnd $iInsertPos = Random(0, 6, 1) ; Randomly choose where to insert the seventh digit. $digits = StringLeft($digits, $iInsertPos) & $lastDigit & StringRight($digits, 6 - $iInsertPos) MsgBox(0, "Random7", $digits) Edited August 22, 2010 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Malkey Posted August 22, 2010 Share Posted August 22, 2010 I added a loop count just to see how many loops if takes to find 7 random digits whose sum is evenly divided by 7. Local $number_string, $the_sum, $next_digit, $LoopCount For $x = 1 To 100 $LoopCount = 0 Do Local $number_string = "" Local $the_sum = "" $LoopCount += 1 For $i = 1 To 7 $next_digit = Random(0, 9, 1) $number_string &= $next_digit $the_sum += $next_digit Next Until Mod($the_sum, 7) = 0 ConsoleWrite("No. of loops = " & $LoopCount & @tab & " to get " & $number_string & " = " & $the_sum & @CRLF) ;MsgBox(0, "Results", $number_string & " = " & $the_sum) Next Link to comment Share on other sites More sharing options...
czardas Posted August 22, 2010 Share Posted August 22, 2010 (edited) I added a loop count just to see how many loops if takes to find 7 random digits whose sum is evenly divided by 7. LMAO: The trial and error approach. It takes on average 10 times longer to do it that way. 4ggr35510n's method is good, but still requires one or two alterations. Calculating the last digit and inserting it at a random position is best.@ pizza, I finally got it working properly. Check the changes I made above. Edited August 22, 2010 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
XKahn Posted August 22, 2010 Share Posted August 22, 2010 I would really love to know the purpose of this... but here is what you do about 4ggr35510n's code. Take the final string of numbers and shuffle them at random. The order won't change the sum so the first and last digit won't be too predictable. Link to comment Share on other sites More sharing options...
4ggr35510n Posted August 22, 2010 Share Posted August 22, 2010 (edited) Ehhh..." The first digit will always be zero "True, my bad, I've misunderstand " If the first number could have a chance to be a zero then that would be a bonus. " :]Here's 100% perfect code: (including fix of "last digit is always < 7" bug)Local $number_string = "" Local $the_sum For $i = 1 to 6 $next_digit = Random(0,9,1) $number_string &= $next_digit $the_sum += $next_digit Next $last_digit = 7 - Mod($the_sum, 7) If $last_digit <= 2 Then $last_digit += 7 * Radom(0,1,1) $number_string &= $last_digit MsgBox(0,"",$number_string)Simpler, isn't it? :] Edited August 22, 2010 by 4ggr35510n Link to comment Share on other sites More sharing options...
czardas Posted August 22, 2010 Share Posted August 22, 2010 Simpler, isn't it? :]I like the modifications you made. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Spiff59 Posted August 22, 2010 Share Posted August 22, 2010 (edited) Here's 100% perfect code Can the last digit ever be 0? A different approach: MsgBox(0, "", SevenBySeven()) Func SevenBySeven() Local $number = Random(0,9999999,1) Local $last_digit = Mod($number, 10) Local $adjust = Mod(Execute(StringRegExpReplace($number, "[0-9]", "\+\0")), 7) ; amount to subtract Select Case $last_digit - $adjust < 0 ; not ok to subtract $adjust -= 7 ; amount to add Case $last_digit - $adjust < 3 ; ok to add (or subtract) $adjust -= (Random(0,1,1) * 7) ; random add or subtract EndSelect Return StringRight( "000000" & $number - $adjust, 7) EndFunc Or maybe just this, if one doesnt care about randomly bumping a midrange last digit either down OR up (resulting in a higher incidence of the last digit being a low number): Func SevenBySeven() Local $number = Random(0,9999999,1) Local $last_digit = Mod($number, 10) Local $adjust = Mod(Execute(StringRegExpReplace($number, "[0-9]", "\+\0")), 7) ; amount to subtract If $last_digit - $adjust < 0 Then $adjust -= 7 ; amount to add Return StringRight( "000000" & $number - $adjust, 7) EndFunc Edit: Just noticed it's 7 lines! SevenBySevenBySeven() ? Edited July 21, 2011 by Spiff59 Link to comment Share on other sites More sharing options...
XKahn Posted August 23, 2010 Share Posted August 23, 2010 Ehhh..." The first digit will always be zero "True, my bad, I've misunderstand " If the first number could have a chance to be a zero then that would be a bonus. " :]Here's 100% perfect code: (including fix of "last digit is always < 7" bug)...Simpler, isn't it? :]Sorry, 4ggr35510n I didn't mean to sound like your code was faulty in any way. I just couldn't help but wonder why Pizza needed such a program to begin with. It sounds rather bizarre. Good job perfecting the code. Link to comment Share on other sites More sharing options...
MvGulik Posted August 23, 2010 Share Posted August 23, 2010 Func SevenBySeven() Local $number = Random(0,9999999,1) Local $last_digit = Mod($number, 10) Local $adjust = Mod(Execute(StringRegExpReplace($number, "[0-9]", "\+\0")), 7) ; amount to subtract If $last_digit - $adjust < 0 Then $adjust = $adjust - 7 ; amount to add Return StringRight( "000000" & $number - $adjust, 7) EndFunc "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... 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