Computer Science Canada

How to match users inputed numbers with random generator numbers

Author:  xBloodyHeartx [ Mon Nov 23, 2015 7:05 pm ]
Post subject:  How to match users inputed numbers with random generator numbers

What is it you are trying to achieve?

I would like to know how to program where the user inputs 4 numbers then next step program generates 4 random numbers then checks if the 4 random numbers generated doesn't not match the users 4 numbers. if 4 random numbers matches the users 4 numbers it re-generates again til it doesn't match users 4 numbers.

example.... user enters 2,4,7,9
num1 := Rand.Int (0,9)
num2 := Rand.Int (0,9)
num3 := Rand.Int (0,9)
num4 := Rand.Int (0,9)
computer generates random numbers num1=1, num2=5, num3=6, num4=9
then checks 1,5,6,9 not= 2,4,7,9
then program exit

I tried to make a short program to test it out but don't know if it is working or not, as well 0 number doesn't come up in the result of random numbers when you input 4 numbers that is not -1.


setscreen ("screen:45;75")

var num1, num2, num3, num4 : int := -1
var input1 : int

put "Enter your numbers"
put "1st number"
get num1
put "2nd number"
get num2
put "3rd number"
get num3
put "4th number"
get num4
put "Enter 1 for next step"
get input1

if input1 = 1 then
loop
cls
var inputrandomnumber : string
var linerandomnumber : int
locate (7, 26)
color (1)
put "4 RANDOM GENERATED NUMBERS"
locate (8, 26)
put "--------------------------"
locate (15, 1)
color (1)
put "How Many Lines Would You Like?"
locate (16, 1)
put "[If Entered 0, Goes Back To The Menu]"
locate (17, 1)
put "="
loop
locate (17, 3)
put ""
locate (17, 3)
color (40)
get inputrandomnumber
if strintok (inputrandomnumber) then
linerandomnumber := strint (inputrandomnumber)
if linerandomnumber >= 0 then
put ""
color (7)
var linesrandomnumber : array 0 .. linerandomnumber of int
var num1randomnumber, num2randomnumber, num3randomnumber, num4randomnumber : int

for linezrandomnumber : 1 .. upper (linesrandomnumber)

loop

num1randomnumber := Rand.Int (0, 5)

loop
num2randomnumber := Rand.Int (0, 5)
if num1randomnumber not= num2randomnumber then
exit
end if
end loop

if (num1randomnumber and num2randomnumber) not= (num1 and num2) then %Random numbers should not match user's numbers%
if (num1randomnumber and num2randomnumber) not= (num3 and num4) then %Random numbers should not match user's numbers%
exit
end if
end if
end loop

put num1randomnumber, " ", num2randomnumber
end for
end if
exit
end if
end loop
exit
end loop
end if

Author:  TokenHerbz [ Wed Nov 25, 2015 3:58 pm ]
Post subject:  Re: How to match users inputed numbers with random generator numbers

First I want to encourage you to take a look at this link, and read up about arrays. This will be extremely helpful for you to solve this problem. Heres the link - http://compsci.ca/v3/viewtopic.php?t=14333

Next, after you get the users numbers and the randoms numbers, you have to compare each of them.

You can run through a double loop to do this, and if theirs a match then you can reroll and recheck the number to make sure they don't match.

Any other questions feel free to post

You can take it a step further to, to make the random number check against not only user numbers but its other numbers and avoid generating those also.

Author:  xBloodyHeartx [ Sun Nov 29, 2015 5:25 pm ]
Post subject:  Re: How to match users inputed numbers with random generator numbers

can I match the numbers in a same line and exact order?

I prefer this way 1 2 3 4 not= 5 6 7 8

instead of
not this way
1 not= 5
2 not= 6
3 not= 7
4 not= 8

Author:  TokenHerbz [ Mon Nov 30, 2015 12:49 am ]
Post subject:  RE:How to match users inputed numbers with random generator numbers

This isn't ruby, it's turing. I suppose if you wanted to write your compare class and implement it you could, but I'm not aware of any built in logic thats going to give you what your looking for in a specific one liner check.

I suppose you could cheat a little but its not a sound proof way, you could take the inputs of answers 1,2,3,4 and check that value is the same as the rand numbers, and then its a one time check but this wont be specific to one number it'll be all of them, which i think defeats your programs question.

So maybe you can post the code you got so far and we can dive in and solve this Smile

Author:  xBloodyHeartx [ Mon Nov 30, 2015 8:14 am ]
Post subject:  Re: How to match users inputed numbers with random generator numbers

which way would be a proper way that would work?

this way

if (num1randomnumber and num2randomnumber) not= (num1 and num2) then
if (num1randomnumber and num2randomnumber) not= (num3 and num4) then
end if
end if

OR

this way

if num1randomnumber not= num1 and
num2randomnumber not= num2
then
if num1randomnumber not= num3 and
num2randomnumber not= num4
then
end if
end if



My program is only missing matching the numbers. Everything else is fine and working, that's why i am focus only on that area.

Author:  TokenHerbz [ Mon Nov 30, 2015 12:51 pm ]
Post subject:  RE:How to match users inputed numbers with random generator numbers

neither, your trying to compare each of the 4 numbers to each of the 4 random numbers to see if there any the same right.

so you want to loop though each, for example if there are 10 numbers each, your not going to write a specific if case for each you want to use a loop.

here is an example you can pick apart. it goes through the logic you need, mind you don't need to do so many checks i guess.

any questions about it just ask -> if you keep running the program you'll notice the random numbers change orders, but they are always the same NON VALUES of the users input.

code:

const AMOUNT_OF_USER_INPUT := 4
var userNums : array 0 .. AMOUNT_OF_USER_INPUT - 1 of int
var randNums : array 0 .. AMOUNT_OF_USER_INPUT - 1 of int

%%Hard code some users numbers
userNums(0) := 2
userNums(1) := 4
userNums(2) := 5
userNums(3) := 7

for i: 0 .. upper(randNums)
    loop
        var flag : boolean := false
        randNums(i) := Rand.Int(0,(AMOUNT_OF_USER_INPUT * 2) - 1)
       
        for j: 0 .. upper(userNums)
            if (randNums(i) = userNums(j)) then
                flag := true
            end if
        end for
        if i > 0 then
            for j: 0 .. i - 1
                if (randNums(i) = randNums(j)) then
                    flag := true
                end if
            end for
        end if
        exit when flag = false
    end loop
end for

put "---"

for i: 0 .. upper(randNums)
    put randNums(i)
end for


: