Jump to content




random code generator


  • You cannot reply to this topic
13 replies to this topic

#1 Nivm200

  • Members
  • 5 posts

Posted 20 June 2013 - 01: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?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 June 2013 - 09:46 PM

Split into new topic.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 20 June 2013 - 11:05 PM

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.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 June 2013 - 11:08 PM

However that being said, everything that you want to do here is completely possible to program.

#5 ChrisAustralia

  • New Members
  • 2 posts

Posted 21 June 2013 - 02:28 AM

View PostNivm200, on 20 June 2013 - 01:41 PM, said:

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.generater...ndom-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

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 June 2013 - 02:37 AM

View PostChrisAustralia, on 21 June 2013 - 02:28 AM, said:

- 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

#7 MR_nesquick

  • Members
  • 106 posts
  • LocationNorway

Posted 21 June 2013 - 05: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


#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 June 2013 - 05:22 AM

View PostMR_nesquick, on 21 June 2013 - 05:13 AM, said:

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 by theoriginalbit, 21 June 2013 - 05:25 AM.


#9 MR_nesquick

  • Members
  • 106 posts
  • LocationNorway

Posted 21 June 2013 - 05:54 AM

View Posttheoriginalbit, on 21 June 2013 - 05:22 AM, said:

View PostMR_nesquick, on 21 June 2013 - 05:13 AM, said:

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

#10 GopherAtl

  • Members
  • 888 posts

Posted 21 June 2013 - 06: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.

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 June 2013 - 06:07 AM

View PostMR_nesquick, on 21 June 2013 - 05:54 AM, said:

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!

View PostMR_nesquick, on 21 June 2013 - 05:54 AM, said:

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:

#12 GopherAtl

  • Members
  • 888 posts

Posted 21 June 2013 - 06: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

#13 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 21 June 2013 - 06: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 :)

#14 ChrisAustralia

  • New Members
  • 2 posts

Posted 27 June 2013 - 10:50 PM

@Nivm200
Have you found a solution yet? Please share with us.
Cheers - Chris





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users