Guessing Game of sorts?

My code is for a “guess the number” minigame’s script:

local numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100}

local random = math.random(1, #numbers)

function chooseNumber(guess)
    while wait() do
        if guess == random then
            print('Correct!')
        else
            print('Wrong!')
        end
        break
    end
end

-- User can guess their number here:

chooseNumber(10)

I think overall the code is pretty good and functions correctly, but a friend was telling me that it’s inefficient and there’s a better way of doing it. Is there any way I could improve it?

8 Likes

I think your friend might not know how to program. There’s not many ways I can think of to make this more efficient than it already is.

4 Likes

What exactly is the point of having a numbers table if you don’t ever reference it again except in the random function? If you were selecting a random number from a specified list of numbers, I could see it but you’re not accessing the list anywhere else.

Point and case - what is the point of:

local numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100}
local random = math.random(1, #numbers) 

as opposed to

local maxNumber = 100 -- or whatever you want the highest number to be
local random = math.random(1, maxNumber) 
5 Likes

Here’s an alternative:

-- Overkill
local NUMBERS = {} do
    for i = 1, 100 do
        table.insert(NUMBERS, i) -- incorrectly capitalized
    end
end
local chosenNumber = NUMBERS[math.random(#NUMBERS)]

-- Best method
local chosenNumber = math.random(100)

-- Best function -- the while loop was not useful
local function guessNumber(number)
    if guess == chosenNumber then
        print("Correct!")
    else
        print("Incorrect.")
    end
end

-- Guessing
guessNumber(9)

If you want a function for randomizing the chosenNumber

local function randomizeNumber()
    chosenNumber = math.random(100)
end
2 Likes

Your numbers table sticks out - you’ve hard coded it for one, where it might be easier in future to generate it with a for loop:

local numbers = {} do
    for i = 1, 100 do
        table.insert(numbers, i)
    end
end

But you’re only using the numbers table to get the maximum number, so you could replace it with a variable local max_number = 100.

Naming your variable random is not good, as it’s a namespace that’s already used. Naming this to random_number or something similar would be an easy fix, but wouldn’t really matter in such a small script.

You also don’t need a while loop for the number guessing, as the input for the function is only one guess, meaning that the loop only runs once. Change this to a simple conditional statement:

function chooseNumber(guess)
    if guess == chosen_number then
        print("Correct!")
    else
        print("Wrong, try again!")
    end
end

Finally, although many people will tell you otherwise using while wait() do is not very good practice. This topic by @colbert2677 links to this Google document which explains why. It’s well worth a read if you want to avoid picking up a bad habit.

4 Likes

I’m not sure how anyone can consider this code efficient. If you think making a table with 100 elements just for the purpose of checking its length is “efficient”, you’re the one who “might not know how to program.” Creating a loop that will only ever run once due to a break is also inefficient and pointless.

2 Likes

First of all, I know C#, C, C++, HTML, Python, HDMI, VGA, and Latex.
I know my stuff when it comes to programming.

The code sent is actually utilizing a micro optimization that causes the code to run 3 nanoseconds faster than other methods. It’s completely undocumented but it does exist and it’s a life saver.

2 Likes

Pretty sure doing while wait() do instead of keeping the code outside a pointless loop makes it wait for a 1/30 of a second which completely negates those non-existent 3 nanoseconds.

1 Like

But you can’t deny that he would’ve lost the 3 nanoseconds if he had used another method.

1 Like

Language, language, language, markup, language, video/audio protocol, video/audio protocol, markup

All in all good troll

3 Likes

I think my code is literally perfect, and I’m insulted people would think otherwise.

1 Like

For the record, I never said your code wasn’t perfect. Just curious on why use the tables. Is it faster, more efficient, or are you planning on using the table in later code?

I use random numbers a lot and I want to know how I can better my own code.

2 Likes

2 posts were merged into an existing topic: Off-topic and bump posts