This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

Deciding Categories

Started by KingofGamesYami, 24 September 2015 - 04:44 PM
KingofGamesYami #1
Posted 24 September 2015 - 06:44 PM
The title is pretty ambiguous, I'm not exactly sure how to word my problem… I have a number that can be anything between 0 and 99. I have a string that has at least 4 characters, separated into groups of 2 (if odd, the last group will be 1). I can find the number of these groups.

My problem is this: I want the first number to determine which of the other 2+ numbers is selected. For example, if I have 2 other numbers (let's say 66 and 25), the first number being below 49 would result in 66 being selected, the number being above 50 would result in 25 being selected.


		local n = tostring( secret:sub( i, i ):byte()^current:sub( i, i ):byte() )
		if #n < 5 then
			new = new .. string.char( tonumber( n:sub( 1, 2 ) ) )
		else
			local choices = math.ceil( #n - 2 )
			--#range = 0, 99
			local decision = tonumber( n:sub( 1, 2 ) )
			--#what do I do here??  I'm lost.
		end

(secret and current are strings, this is in a loop)
Exerro #2
Posted 24 September 2015 - 08:15 PM
Probably missed something here but surely a simple if statement will do? 'if n < 50 then (66) else (25) end'
Edited on 24 September 2015 - 06:15 PM
KingofGamesYami #3
Posted 24 September 2015 - 08:30 PM
Yes, what you missed was the different categories will be variable. For example, I might end up with 3 categories - below 33, above 66, and between 33 and 66.
Exerro #4
Posted 24 September 2015 - 10:39 PM
Will they be evenly spaced categories?
KingofGamesYami #5
Posted 24 September 2015 - 11:54 PM
Yes, each should have an equal "chance" of being picked.
Bomb Bloke #6
Posted 25 September 2015 - 01:37 AM
Take the number 99 (or whatever your maximum is), divide by the number of categories, assign the result to eg "range".

Take your "first number", divide by "range". Round the answer down, use this new result to index into your categories.
KingofGamesYami #7
Posted 25 September 2015 - 02:00 AM
Thanks, it's working (…as far as I can tell) :)/>
Bomb Bloke #8
Posted 25 September 2015 - 02:31 AM
Come to think of it, weren't you explaining this sort of percentage math just the other day? :P/> ;)/>