Hi guys,
It's 1:28 am, and I've got an issue with a program that's going to bug me all night if I don't solve it.
The goal is simple, randomly chose 1 number, then 10 more. take the first and detect which of the 10 is a match or the closest match to the first number. I'm trying to do it as simply as possible without any arrays, and only basic logic. Been going over this with another guy and neither of us can work out why the program isn't working. We're both likely doing something really stupid so if anyone else can shine a light on it I'd be very greatful.
The code is:
//select random number, this is the number we compare to the rest
lucky = random( 0, 100 )
//generate random numbers
rnd1 = random( 0, 100 )
rnd2 = random( 0, 100 )
rnd3 = random( 0, 100 )
rnd4 = random( 0, 100 )
rnd5 = random( 0, 100 )
rnd6 = random( 0, 100 )
rnd7 = random( 0, 100 )
rnd8 = random( 0, 100 )
rnd9 = random( 0, 100 )
rnd10 = random( 0, 100 )
//We want the shortest number distence between lucky and the test number.
//To test this, we check to see if the first random number is greater than lucky, if it is then we calculate test so it generates a positive value, otherwise we calculate it in reverse
If rnd1 > lucky
test1 = rnd1 - lucky
Else
test1 = lucky - rnd1
EndIf
//This is repeated for all the numbers.
If rnd2 > lucky
test2 = rnd2 - lucky
Else
test2 = lucky - rnd2
EndIf
If rnd3 > lucky
test3 = rnd3 - lucky
Else
test3 = lucky - rnd3
EndIf
If rnd4 > lucky
test4 = rnd4 - lucky
Else
test4 = lucky - rnd4
EndIf
If rnd5 > lucky
test5 = rnd5 - lucky
Else
test5 = lucky - rnd5
EndIf
If rnd6 > lucky
test6 = rnd6 - lucky
Else
test6 = lucky - rnd6
EndIf
If rnd7 > lucky
test7 = rnd7 - lucky
Else
test7 = lucky - rnd7
EndIf
If rnd8 > lucky
test8 = rnd8 - lucky
Else
test8 = lucky - rnd8
EndIf
If rnd9 > lucky
test9 = rnd9 - lucky
Else
test9 = lucky - rnd9
EndIf
If rnd10 > lucky
test10 = rnd10 - lucky
Else
test10 = lucky - rnd10
EndIf
//Now we need the smallest number, we assume first that 1 is the closest match, then proceed to test the rest.
result = 1
//here we say, if test is larger than test2 then test2 holds the shortest distence between lucky and that random number. Otherwse result remains at one, if test one is bigger we set the result
//to 2, and then repeat the process for the remaining numbers.
if test1 > test2 then result = 2
if test2 > test3 then result = 3
if test3 > test4 then result = 4
if test4 > test5 then result = 5
if test5 > test6 then result = 6
if test6 > test7 then result = 7
if test7 > test8 then result = 8
if test8 > test9 then result = 9
if test9 > test10 then result = 10
//this just displays the generated results.
Do
print( "This is the lucky number: " + str( lucky ) )
print( "The following are guesses by the computer!" )
print( "1. " + str( rnd1 ) + " test value " + str( test1 ) )
print( "2. " + str( rnd2 ) + " test value " + str( test2 ) )
print( "3. " + str( rnd3 ) + " test value " + str( test3 ) )
print( "4. " + str( rnd4 ) + " test value " + str( test4 ) )
print( "5. " + str( rnd5 ) + " test value " + str( test5 ) )
print( "6. " + str( rnd6 ) + " test value " + str( test6 ) )
print( "7. " + str( rnd7 ) + " test value " + str( test7 ) )
print( "8. " + str( rnd8 ) + " test value " + str( test8 ) )
print( "9. " + str( rnd9 ) + " test value " + str( test9 ) )
print( "10. " + str( rnd10 ) + " test value " + str( test10 ) )
print( "This is the closest guess by the computer." )
print( result )
Sync()
Loop