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

Need help with blackjack coding

Started by Zombie_brine, 17 September 2012 - 07:36 PM
Zombie_brine #1
Posted 17 September 2012 - 09:36 PM
I need some code that would output a random number of redstone pulses between 1-13. Ill credit you in the video i will eventually make.
Cranium #2
Posted 17 September 2012 - 10:00 PM
You can use math.random(1,13) –1 being the start point, 13 being the end point.
Implemented like this:

local pulse = math.random(1,13)
for i = 1,#pulse do
rs.setOutput("side",true)
sleep(.5)
rs.setOutput("side",false)
end
MysticT #3
Posted 17 September 2012 - 11:26 PM
You can use math.random(1,13) –1 being the start point, 13 being the end point.
Implemented like this:

local pulse = math.random(1,13)
for i = 1,#pulse do
rs.setOutput("side",true)
sleep(.5)
rs.setOutput("side",false)
end
Don't forget to add a sleep after setting the output to false, or it won't work. Also, pulses is a number, so you can't use the lenght operator on it.

local pulses = math.random(1, 13)
for i = 1, pulses do
  rs.setOutput("side", true)
  sleep(0.2)
  rs.setOutput("side", false)
  sleep(0.2)
end
Cranium #4
Posted 17 September 2012 - 11:35 PM
Ah yes, you are right. I am so used to working with tables on the for loops, I didn't even think about that. Also, you wouldn't need to add the sleep after false, because once it finishes, it will restart the loop. So unless he is wanting something to stay off for a long period of time, he can leave the second sleep() out of there. It's an either/or situation.
MysticT #5
Posted 17 September 2012 - 11:38 PM
Actually, if you don't put the second sleep it won't make pulses, because it will turn it on, wait some time, turn it off and immediately turn it on, so the redstone is not updated and there's no pulse, just a continuous redstone signal.
Cranium #6
Posted 17 September 2012 - 11:41 PM
Stop making arguments with LOGIC!!!! You're right, I'm wrong. Haha, I am not on a roll today. :)/>/>
Zombie_brine #7
Posted 18 September 2012 - 12:01 AM
whenever i try this i get
"deal:2: attempt to get length of number"
as an error
Cranium #8
Posted 18 September 2012 - 12:13 AM
Read MysticT's post. He is correct on this one. I was going from a slow work day, not thinking. Ha…
Zombie_brine #9
Posted 18 September 2012 - 12:43 AM
coppied code from mystic verbatum -

"deal:3: bad argument: String expected, got nil"
Ideas..
Lyqyd #10
Posted 18 September 2012 - 01:30 AM
Are you sure it was verbatim? Sounds like you did:


rs.setOutput(back, true)

…instead of :


rs.setOutput("back", true)

Or whatever the side was.
Zombie_brine #11
Posted 18 September 2012 - 02:54 AM
I had to rethink the system.. how would i make it 1-10 but have 10 in the pool 4 times (10,j,q,k)
Zombie_brine #12
Posted 18 September 2012 - 03:08 AM
"I had to rethink the system.. how would i make it 1-10 but have 10 in the pool 4 times (10,j,q,k)"
Not sure if this would work but…
local pulse = math.random(1,10,10,10,10)
Would this have the desired effect?
GopherAtl #13
Posted 18 September 2012 - 05:02 AM
nope, but this would

local numPulses = math.min(math.random(1,13),10)

min() returns the smallest of it's params. 1-10 are unchanged, 11-13 are changed to 10.
Zoinky #14
Posted 18 September 2012 - 06:52 AM
Erm….

local pulses = math.random(1, 13)
Get rid of the space.

local pulses = math.random(1,13)
I think?
Lyqyd #15
Posted 18 September 2012 - 02:39 PM
Erm….

local pulses = math.random(1, 13)
Get rid of the space.

local pulses = math.random(1,13)
I think?

The space is fine. Spacing arguments out can help improve readability.
Zombie_brine #16
Posted 19 September 2012 - 03:00 AM
nope, but this would

local numPulses = math.min(math.random(1,13),10)

min() returns the smallest of it's params. 1-10 are unchanged, 11-13 are changed to 10.
replaced the line, "deal :2: "for" limit must be a number"
Worked before i changed just that line…
Lyqyd #17
Posted 19 September 2012 - 03:47 PM
That new line has numPulses as the name of the variable, the loops mentioned earlier use pulses as the name. Ensure that they match and that error should go away.