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

Emit Redstone Signal After Random Time

Started by tiwuno, 20 September 2013 - 03:15 PM
tiwuno #1
Posted 20 September 2013 - 05:15 PM
Just wondering how I can code a computer to emit a redstone signal between 0 and 604,800 seconds (1 week). I know the code exists, but I'm fairly n00bish with computers, so I figured it's be easier to ask here than try and figure it out myself. Any help would be greatly appreciated, thanks in advance!
Cranium #2
Posted 20 September 2013 - 05:23 PM
Just use sleep with an interval:

while true do 
    sleep(604800)
    rs.setOutput("side", true)
    sleep(.05)
    rs.setOutput("side", false)
end
Bubba #3
Posted 20 September 2013 - 05:45 PM
Don't most servers restart? If so, you'll need to keep track of how much time has passed. Look into the fs api in order to write data to file. Keep in mind that when reading the number back from a file, you'll need to use tonumber(string_returned_by_file) in order to convert it to an actual number you can use.
tiwuno #4
Posted 20 September 2013 - 08:49 PM
Just use sleep with an interval:

while true do 
    sleep(604800)
    rs.setOutput("side", true)
    sleep(.05)
    rs.setOutput("side", false)
end

Will that be randomized?

Don't most servers restart? If so, you'll need to keep track of how much time has passed. Look into the fs api in order to write data to file. Keep in mind that when reading the number back from a file, you'll need to use tonumber(string_returned_by_file) in order to convert it to an actual number you can use.

This is for a single player world. Also, real-game time or played time doesn't matter, whichever is easier.
Bubba #5
Posted 20 September 2013 - 09:48 PM
Will that be randomized?
No… do you want it randomized? If so, this becomes significantly more easy.

This is for a single player world.

So you never shut Minecraft down? If you shut Minecraft down, the computers shutdown and all programs stop running.

Also, real-game time or played time doesn't matter, whichever is easier.

That's not what I meant with tonumber. tonumber will convert the data type string to the data type number. A string is simply the representation of characters which you cannot use mathematical operators such as + and - on. A number allows you to actually do math.

Anyway, if you want randomized, that's pretty simple because you don't need to keep track of how much time is left. Simply do something along the lines of this:

while true do --# Loop infinitely (or until the computer is terminated/shutdown)
  local gen = math.random(0, 604800) --# Generator a random number that we will wait for
  sleep(gen) --# Actually do the waiting
  rs.setOutput("[side]", true) --# Set the redstone output to true after we're done waiting
  sleep(1) --# Wait for a second
  rs.setOutput("[side]", false) --# Set the redstone output to false
end
tiwuno #6
Posted 21 September 2013 - 12:51 PM
So you never shut Minecraft down? If you shut Minecraft down, the computers shutdown and all programs stop running.

Ah, I wasn't aware that computers reset themselves if the game/server was shut down, apologies. If I wanted to keep track of how much game time had elapsed since the program was first run (session-persistent, I believe it's called?) how much more complicated would the code be?
CCJJSax #7
Posted 21 September 2013 - 04:24 PM
Will that be randomized?
No… do you want it randomized? If so, this becomes significantly more easy.

This is for a single player world.

So you never shut Minecraft down? If you shut Minecraft down, the computers shutdown and all programs stop running.

Also, real-game time or played time doesn't matter, whichever is easier.

That's not what I meant with tonumber. tonumber will convert the data type string to the data type number. A string is simply the representation of characters which you cannot use mathematical operators such as + and - on. A number allows you to actually do math.

Anyway, if you want randomized, that's pretty simple because you don't need to keep track of how much time is left. Simply do something along the lines of this:

while true do --# Loop infinitely (or until the computer is terminated/shutdown)
  local gen = math.random(0, 604800) --# Generator a random number that we will wait for
  sleep(gen) --# Actually do the waiting
  rs.setOutput("[side]", true) --# Set the redstone output to true after we're done waiting
  sleep(1) --# Wait for a second
  rs.setOutput("[side]", false) --# Set the redstone output to false
end

That's still technically only pseudo random. I'm not sure if this is possible within computercraft, but to make it much more unpredictable you should use this. This is off the top of my head, but I think it's right.



math.randomseed(os.time())
math.random(0, 604800)

Edited by
Bubba #8
Posted 21 September 2013 - 04:32 PM
-snip-

Err no one is worried about security here, and the numbers are all going to be psuedo-random no matter what seed you give it. Not to mention, I highly doubt that anyone here could reverse the numbers without a significant amount of work.

Oh and if you were going to use randomseed, os.time() isn't even going to give you a different value usually. Check this out:

for i=1,3 do
  math.randomseed(os.time())
  print(math.random())
  sleep(1)
end

The output for this is:
0.73105735
0.73105735
0.73105735

In order to get better random results, you'd probably want to multiply the result of os.time by some factor, though I haven't tested it.

So yeah. Don't bother seeding unless you're storing something like your social security number (which I do not advise, by the way).
tiwuno #9
Posted 22 September 2013 - 07:09 PM
Yea I'm not really worried about security. So how do I go about making a session-persistent randomized timer? Played time would be preferable to real time.
campicus #10
Posted 23 September 2013 - 08:12 PM
Yea I'm not really worried about security. So how do I go about making a session-persistent randomized timer? Played time would be preferable to real time.

You will need to have a good look at the fs api. This will allow you to store a variable (or more than one) in a file, which can be retrieved on restart.

EDIT: I learnt persistence by looking at other peoples mining programs.