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

random code generator

Started by Nivm200, 20 June 2013 - 12:41 PM
Nivm200 #1
Posted 20 June 2013 - 02:41 PM
Hello!
Before a few days i played FTB and made something…
Now … I wonder , is there any way to make random code generator (numbers+ ABCD… and with capitals)
That will save the code untill somone use that code and then delete that code?
1 computer will generate the code while redstone signal/the owner will tell it and 1
computer will recive that code and check if the input = to the code and then delete the code…
if its possible - can it have multiply codes & uses to the codes?
For example - I made map and i want that people will put diamond per code that have 10 uses or
Iron/coal per 1 enter… is that possible?
Lyqyd #2
Posted 20 June 2013 - 10:46 PM
Split into new topic.
Bomb Bloke #3
Posted 21 June 2013 - 12:05 AM
Are you trying to make some sort of password system, a slot machine, or a lottery…?

Having the server send a copy of a password to a client system is not exactly a secure way of doing things, if that's what you're aiming to do.
theoriginalbit #4
Posted 21 June 2013 - 12:08 AM
However that being said, everything that you want to do here is completely possible to program.
ChrisAustralia #5
Posted 21 June 2013 - 03:28 AM
Now … I wonder , is there any way to make random code generator (numbers+ ABCD… and with capitals)
That will save the code untill somone use that code and then delete that code?
1 computer will generate the code while redstone signal/the owner will tell it and 1
computer will recive that code and check if the input = to the code and then delete the code…
if its possible - can it have multiply codes & uses to the codes?
For example - I made map and i want that people will put diamond per code that have 10 uses or
Iron/coal per 1 enter… is that possible?

How I would do it…

Here is a good free random code generator, http://www.generaterandomcodes.com/free-generate-random-codes-tool
Limited to 20K, but you can use prefixes and run it many times ;)/>
- if that's not enough, then maybe use md5 with simple string and number increments ?:-/

Dump codes in mysql db using phpmyadmin

php script to receive ajax request, but need to make sure request comes from your own scripts, either with api key or some other way. HTTPS helps.

if the code is found, add code to "used_codes" table and delete from master_codes table.

If you want "10 uses" then keep another field "uses", increment each time. mysql can increment a field for you. Then only move to used codes table when it reaches 10

Issues I can see;
Validating source of request, only need this if you expect hack attempts.
Catering for Errors, eg. if you send "success" response. Did the app actually received and used that response. Probably want another ping back to server to say, received success.

Hard to suggest any more based on your post.

Hope that helps
theoriginalbit #6
Posted 21 June 2013 - 03:37 AM
- snip -
That is quite a lot of work and requires hardware that not everyone has (i.e. a server with a DBMS). This entire project is all possible within Lua, so doing so would be the better, and easier option.

Simplest method…

— Create a table, or load a table from a file, that contains a list of all the currently used strings and their number of uses remaining

— Generate a random string, from something like a validChars list, making sure the string doesn't appear in the table of used strings

local validChars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

— Store the random string into a table with the number of uses

— Save the uses table into a file for restart persistence

— Present the random string to a user

— When the string is used, check the table of uses, if it has 0 uses, return a failure back, when it has uses, decrement the uses and return success
MR_nesquick #7
Posted 21 June 2013 - 06:13 AM
one easy way to get a random number or letter from a table.

local table = {"1","2","3","4",}

for i = 1,"how long you want you're password" do
A = table[math.random(tonumber(#table))]
print(A)
end
theoriginalbit #8
Posted 21 June 2013 - 06:22 AM
one easy way to get a random number or letter from a table.

local table = {"1","2","3","4",}

for i = 1,"how long you want you're password" do
A = table[math.random(tonumber(#table))]
print(A)
end
Wouldn't quite work like you expected. This would only be 1 character long (assuming "how long you want you're password" was replaced with a number), as A is always overridden. Also #table is always going to be a number, there is no need to call tonumber on it…

This would be the method I would use.


--# a table to store all the used codes, and their uses
local usedCodes = {}
--# all the valid characters that can be used
local validChars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

local function generateCode(length, uses)
  --# make sure only this function can access it
  local code
  --# repeat until a code that hasn't been used is found
  repeat
	--# start with an empty string
	code = ""
	--# loop for the desired length
	for i = 1, length do
	  --# pick a random index for the character in the validChars string
	  local idx = math.random(#validChars)
	  --# append the randomly chosen character
	  code = code..validChars:sub(idx,idx)
	end
  --# when it has not been used, finish, else try again
  until not usedCodes[code]
  --# add the max uses into the table, if `uses` isn't provided assume 1
  usedCodes[code] = uses or 1
  --# return the resulting code
  return code
end

Edited on 21 June 2013 - 04:25 AM
MR_nesquick #9
Posted 21 June 2013 - 06:54 AM
one easy way to get a random number or letter from a table.

local table = {"1","2","3","4",}

for i = 1,"how long you want you're password" do
A = table[math.random(tonumber(#table))]
print(A)
end
Wouldn't quite work like you expected. This would only be 1 character long (assuming "how long you want you're password" was replaced with a number), as A is always overridden. Also #table is always going to be a number, there is no need to call tonumber on it…

it was more a example how to select a random number or letter from a table. and tonumber(#table) is used to check how long the table is, so you can expand the table in the future without changing you're code
GopherAtl #10
Posted 21 June 2013 - 07:05 AM
mr, but the # operator returns a number. tonumber(#myTable) is always redundant, as #myTable is already a number, not a string that needs to be converted to a number.
theoriginalbit #11
Posted 21 June 2013 - 07:07 AM
it was more a example how to select a random number or letter from a table
Giving an example that half works is not what we are here to do when answering questions. There is an expected level of quality and correctness we must adhere to, just the same as when we are making tutorials for the Tutorials section of these forums. Yes from time to time we may get the odd fact here and there wrong, or have a slight bug in our code. But the other 99.99% of the time the code we supply is bug free, otherwise what kind of example and help are we if the code we supply to help out is riddled with bugs. Examples are great, examples without bugs are excellent!

and tonumber(#table) is used to check how long the table is, so you can expand the table in the future without changing you're code
I know what you were trying to do there, and I can tell your right now that using # on the table will give you a number, you do not need to use tonumber to convert a number, to a number. It is a redundant function call.
EDIT: Oh ninja's, GopherAtl you're good at ninja'in me lately! :ph34r:/>
GopherAtl #12
Posted 21 June 2013 - 07:12 AM
just paying back what I've gotten, I've seen the "new post added" popup dealie while writing answers sooo many times in the last week… XD
TheOddByte #13
Posted 21 June 2013 - 07:29 AM
Uhmm… I have made a program that generates a password as long as you'd like etc.
If you'd like I could send it to you and you could see if it's helpful..

And as those above me as stated it isn't to hard to generate a random password..
Here's an example with just numbers

--Here's what can be in the password
numbers = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
}

--And let's save that into a file called "Password" and the password lenght will be 10
file = fs.open("Password","w")

for i = 1,10 do
n = numbers[math.random(1, #numbers)]
file.write(n)
end
file.close()

Typed this on my phone so I'm not 100% it works..
But I'm pretty sure it does :)/>
ChrisAustralia #14
Posted 27 June 2013 - 11:50 PM
@Nivm200
Have you found a solution yet? Please share with us.
Cheers - Chris