Jump to content

Check if number == rnd


BriGhtx3

Recommended Posts

local z1 = 10 
local rnd1, rnd2, rnd3 = gR() 
local zahl = 0 
if ( tonumber ( z1 ) == tonumber ( rnd1 ) ) or ( tonumber ( z1 ) == tonumber ( rnd2 ) ) or ( tonumber ( z1 ) == tonumber ( rnd3 ) ) then 
    zahl = zahl + 1 
else 
    outputChatBox("Zahl 1: Falsch!") 
end 

gR():

local rnd1 = math.random ( 1, 12 ) 
    local rnd2 = math.random ( 1, 12 ) 
    local rnd3 = math.random ( 1, 12 ) 
return rnd1, rnd2, rnd3 

Hey^^

I try to check if one of the random numbers is the same like z1.

But even if it is the same,it outputs "Zahl 1: Falsch!".

I tried to output rnd1, rnd2, rnd3 and z1 and this works.

Link to comment
local z1 = 10 
    local rnd1, rnd2, rnd3 = gR() 
    local zahl = 0 
    if ( (tonumber ( z1 ) == tonumber ( rnd1 ))  or ( tonumber ( z1 ) == tonumber ( rnd2 ) ) or ( tonumber ( z1 ) == tonumber ( rnd3 ) )) then 
        zahl = zahl + 1 
    else 
        outputChatBox("Zahl 1: Falsch!") 
    end 

Try this.

Edited by Guest
Link to comment

simply :

  
function gr() 
    return math.random ( 1, 12 ) 
end 
  
function something() 
    local someotherthing = 10 
    local someanotherthing = 0 
    for i=1,3 do 
        local random = gr() 
        if someotherthing == random then 
            someanotherthing = someanotherthing + 1 
        else 
             outputChatBox("Whatever is it , Falsch!") 
        end 
    end 
end 
  

Link to comment

I tested it right now on Lua Command Line and it worked perfectly.

Basically, same code:

function gR( ) 
    return math.random ( 1, 12 ), math.random ( 1, 12 ), math.random ( 1, 12 ); 
end 
  
function test ( ) 
    local z1 = 10; 
    local rnd1, rnd2, rnd3 = gR( ); 
    local zah1 = 0; 
    print ( rnd1, rnd2, rnd3 ); 
    if ( tonumber ( z1 ) == tonumber ( rnd1 ) or tonumber ( z1 ) == tonumber ( rnd2 ) or tonumber ( z1 ) == tonumber ( rnd3 ) ) then 
        zah1 = zah1 + 1; 
        print ( 'You won $250.000.000!' ); 
    else 
        print ( 'Zah1 1: Falsch!' ); 
    end 
end 
  
--[[ 
  
    First try: 
     
        test( ); 
         
        Output: 
             
            1   7   3 
            Zah1 1: Falsch! 
             
        -- 
  
    Second try: 
     
        test( ); 
         
        Output:  
         
            10  8   6 
            You won $250.000.000! 
             
        -- 
         
]] 

As you can see, it worked perfectly. I don't understand your problem.

Link to comment
@Stanley you didn't change anything..

@Draken

I generate a number between 1 and 12. And I want to check if one of those randoms is 10.

It is for a lottery to check how many right numbers he picked.

Yes, I did. Look at line 4. I added a "(". Look at your code and will see why I added this.

Link to comment

@Stanley but you forgot to add the ")". And this doesn't change anything ;)

@Draken I also don't understand this problem. My code should work fine, but it doesn't.

I have 3 numbers (z1,z2,3) and I check it like this:

  
if ( tonumber ( z1 ) == tonumber ( rnd1 ) or tonumber ( z1 ) == tonumber ( rnd2 ) or tonumber ( z1 ) == tonumber ( rnd3 ) ) then 
        zahl1 = zahl1 + 1; 
        print ( 'You won $250.000.000!' ); 
    else 
        print ( 'Zah1 1: Falsch!' ); 
    end 
if ( tonumber ( z2 ) == tonumber ( rnd1 ) or tonumber ( z2 ) == tonumber ( rnd2 ) or tonumber ( z2 ) == tonumber ( rnd3 ) ) then 
        zahl1 = zahl1 + 1; 
        print ( 'You won $250.000.000!' ); 
    else 
        outputChatBox( 'Zahl 2: Falsch!' ); 
    end 
if ( tonumber ( z3 ) == tonumber ( rnd1 ) or tonumber ( z3 ) == tonumber ( rnd2 ) or tonumber ( z3 ) == tonumber ( rnd3 ) ) then 
        zahl1 = zahl1 + 1; 
        outputChatBox( 'You won $250.000.000!' ); 
    else 
        outputChatBox( 'Zahl 3: Falsch!' ); 
    end 
  

But it simply doesn't work :/

Link to comment

I tested it and it's working.

zahl1 = 0; 
z1 = 10; 
z2 = 10; 
z3 = 10; 
rnd1 = math.random ( 1, 12 ); 
rnd2 = math.random ( 1, 12 ); 
rnd3 = math.random ( 1, 12 ); 
  
if ( z1 == rnd1 or z1 == rnd2 or z1 == rnd3 ) then 
    zahl1 = zahl1 + 1; 
    print ( 'You won $250.000.000!' ); 
else 
    print ( 'Zah1 1: Falsch!' ); 
end 
if ( z2 == rnd1 or z2 == rnd2 or z2 == rnd3 ) then 
    zahl1 = zahl1 + 1; 
    print ( 'You won $250.000.000!' ); 
else 
    print( 'Zahl 2: Falsch!' ); 
end 
if ( z3 == rnd1 or z3 == rnd2 or z3  == rnd3 ) then 
    zahl1 = zahl1 + 1; 
    print( 'You won $250.000.000!' ); 
else 
    print( 'Zahl 3: Falsch!' ); 
end 
  

Link to comment
  • MTA Team

Why does it have to be so difficult and hardcored script?

local winner = 10 
  
addCommandHandler("lottery", 
     function(player, cmd) 
          local random = tonumber(math.random(1,12)) 
          if random == winner then 
               outputChatBox("You won, congratulations.", player, 0, 255, 0, false) 
          else 
               outputChatBox("Better luck next time.", player, 255, 0, 0, false) 
          end 
     end 
) 
  

Done.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...