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

[1.43] Random Number Generator

Started by aSdtIHN0dXBpZA==, 25 October 2012 - 02:11 PM
aSdtIHN0dXBpZA== #1
Posted 25 October 2012 - 04:11 PM
This likely won't get anywhere, but here it is. I've been working on yesterday, a random number generator.
I have updated it recently to only work on turtles, but output letters, and make the turtle move. Later, yes, I wll add a ton of more modes. For now, I just want to make sure it works, and it works well and works like I wanted it. Still, it wasn't how I wanted it, but it's good enough.


SPECIAL EDITION, Turtle Maze v.0.1337 (Lasts for the first hour): http://pastebin.com/T4GcZjgh
Turtle Maze v.0.4: http://pastebin.com/mXsCXkN2


Older Versions:
Spoilerv.0.1: Mlf
v.0.2: mlf
v.0.3: http://pastebin.com/1Nu5b1x3
v.0.4: http://pastebin.com/mXsCXkN2
jag #2
Posted 25 October 2012 - 04:26 PM
Why do you got
sleep(0)
remiX #3
Posted 25 October 2012 - 04:26 PM
Make it so you have type a string or a decimal number in anywhere it fails and restarts. ^_^/>/>
aSdtIHN0dXBpZA== #4
Posted 25 October 2012 - 04:28 PM
sleep(0)
About half-a-second delay between outputs, so it doesn't crash.
Make it so you have type a string or a decimal number in anywhere it fails and restarts. ^_^/>/>
So, you can terminate it with the press of any key? I'll have to look into that, but it'll happen.
remiX #5
Posted 25 October 2012 - 04:34 PM
sleep(0)
About half-a-second delay between outputs, so it doesn't crash.

If I'm not mistake, for loops don't need pauses to stop crashes.

Make it so you have type a string or a decimal number in anywhere it fails and restarts. ^_^/>/>
So, you can terminate it with the press of any key? I'll have to look into that, but it'll happen.

No. If you were to type a string, such as 'asd' into where it asks for range. It will give an error saying that number expected, got string.
jag #6
Posted 25 October 2012 - 04:35 PM
sleep(0)
About half-a-second delay between outputs, so it doesn't crash.
Well for loops dosen't crash, it's while and repeat loops that does..
jag #7
Posted 25 October 2012 - 04:37 PM
& Upon large (any, but rarer) maximum number inputs, sometimes the output says "nan" between outputs.
NaN = Not a number
Self explanatory…
aSdtIHN0dXBpZA== #8
Posted 25 October 2012 - 04:42 PM
& Upon large (any, but rarer) maximum number inputs, sometimes the output says "nan" between outputs.
NaN = Not a number
Self explanatory…

Yup, I know that. Trying to fix it now, i might release a quick bufix patch soon…

In the meantime, http://pastebin.com/CpNxixXF is the Turtle Movement number generator, which randomly makes a turtle move, better than built-in dance, and also outputs the movements as letters.

sleep(0)
About half-a-second delay between outputs, so it doesn't crash.
Well for loops dosen't crash, it's while and repeat loops that does..

Oh, I thought all crashed.
remiX #9
Posted 25 October 2012 - 04:46 PM
Why not make turtle go up and down? ^_^/>/>
aSdtIHN0dXBpZA== #10
Posted 25 October 2012 - 04:49 PM
Why not make turtle go up and down? ^_^/>/>

http://pastebin.com/1Nu5b1x3
It also digs, provided you use a Pickaxe Turtle.
jag #11
Posted 25 October 2012 - 04:58 PM
Well your code got too many sleep(0).

You could make it "shorter" by doing:
(I'm not sure if you can have functions in tables, and because the minecraft login is down I can't test it)
print("I will dance now. RUN FOR YOUR LIFE!!!!!")
sleep(2.5)
local r = 0
local moves = {
  {char="f",move=turtle.forward}
  {char="b",move=turtle.back}
  {char="l",move=turtle.turnLeft}
  {char="r",move=turtle.turnRight}
  {char="u",move=turtle.up}
  {char="d",move=turtle.down}
}
while true do
  r = math.random(1, #moves)
  write(moves.[r].char)
  moves.[r].move()
end
Because the moves takes time to operate the loop wont crash.

EDIT: The login is back up again!
aSdtIHN0dXBpZA== #12
Posted 25 October 2012 - 05:03 PM
Well your code got too many sleep(0).

You could make it "shorter" by doing:
(I'm not sure if you can have functions in tables, and because the minecraft login is down I can't test it)
print("I will dance now. RUN FOR YOUR LIFE!!!!!")
sleep(2.5)
local r = 0
local moves = {
  {char="f",move=turtle.forward}
  {char="b",move=turtle.back}
  {char="l",move=turtle.turnLeft}
  {char="r",move=turtle.turnRight}
  {char="u",move=turtle.up}
  {char="d",move=turtle.down}
}
while true do
  r = math.random(1, #moves)
  write(moves.[r].char)
  moves.[r].move()
end
Because the moves takes time to operate the loop wont crash.

EDIT: The login is back up again!

Thanks. I've never really understood tables and their positive features, so I never really used them. I'll probably add this in the next update. Ah, what the heck, I'll do it now.
remiX #13
Posted 25 October 2012 - 05:05 PM
Well your code got too many sleep(0).

You could make it "shorter" by doing:
(I'm not sure if you can have functions in tables, and because the minecraft login is down I can't test it)
print("I will dance now. RUN FOR YOUR LIFE!!!!!")
sleep(2.5)
local r = 0
local moves = {
  {char="f",move=turtle.forward}
  {char="b",move=turtle.back}
  {char="l",move=turtle.turnLeft}
  {char="r",move=turtle.turnRight}
  {char="u",move=turtle.up}
  {char="d",move=turtle.down}
}
while true do
  r = math.random(1, #moves)
  write(moves.[r].char)
  moves.[r].move()
end
Because the moves takes time to operate the loop wont crash.

EDIT: The login is back up again!

Tables have so many uses, it's just AMAZING. lol
aSdtIHN0dXBpZA== #14
Posted 25 October 2012 - 05:21 PM
Jag, yew bwoke it!
fix ite!

Nah, don't worry, I'll fix it. I'll bore myself to death reading about tables, and then fix it myself.
jag #15
Posted 25 October 2012 - 05:24 PM
Don't get too comfortable with this solution!
It didn't work!!

I tried multiple ways to solve the error but nothing worked…

Error:
bios:338: [string "newDance"]:6: '}'
expected (to close '{' at line 6)
aSdtIHN0dXBpZA== #16
Posted 25 October 2012 - 05:26 PM
Don't get too comfortable with this solution!
It didn't work!!

I tried multiple ways to solve the error but nothing worked…

Error:
bios:338: [string "newDance"]:6: '}'
expected (to close '{' at line 6)

This is why, I stick with simplicity. Even if it's long and boring, it's just easier. I shall revert the code to an earlier version. I will never do tables, unless the sciprt absolutely completely urgently utterly requires it.
jag #17
Posted 25 October 2012 - 05:56 PM
Hey you know what?
It was a derpy error!!
I forgot the comma ( , ) between the tables…

I got another error now that I'm trying to solve
bios:338: [string "newDance"]:14:
'<name>' expected
aSdtIHN0dXBpZA== #18
Posted 25 October 2012 - 06:20 PM
Your code, your problem. ^_^/>/>


That's simple, isn't it? On line 14 somewhere, you didn't supply a name or something like that for a variable, is that not it?
cant_delete_account #19
Posted 25 October 2012 - 07:31 PM
Hey you know what?
It was a derpy error!!
I forgot the comma ( , ) between the tables…

I got another error now that I'm trying to solve
bios:338: [string "newDance"]:14:
'<name>' expected
You failed here:

  write(moves.[r].char)
  moves.[r].move()
It should be:

  write(moves[r].char)
  moves[r].move()
If I'm correct.
Orwell #20
Posted 25 October 2012 - 07:40 PM
sleep(0)
About half-a-second delay between outputs, so it doesn't crash.
Well for loops dosen't crash, it's while and repeat loops that does..


for i=1,math.huge do
  print(i)
end

It has nothing to do with the kind of loop. Technically you could write a huge script that runs long enough to crash the program. It's just that it runs to long without yielding, regardless of how it runs.

Edit:
If you find that I'm cheating because this technically is 'while true do', this will still crash:

for i=1,10^6 do
  print(i)
end
jag #21
Posted 25 October 2012 - 10:38 PM
Hey you know what?
It was a derpy error!!
I forgot the comma ( , ) between the tables…

I got another error now that I'm trying to solve
bios:338: [string "newDance"]:14:
'<name>' expected
You failed here:

  write(moves.[r].char)
  moves.[r].move()
It should be:

  write(moves[r].char)
  moves[r].move()
If I'm correct.
You are correct!!!
Either you do
moves[r].move()
or
moves[r]["move"]()

So the final code:
(Also on pastebin: uVSjref1)
print("I will dance now. RUN FOR YOUR LIFE!!!!!")
sleep(2.5)
local r = 0
local moves = {
  {char="f",move=turtle.forward},
  {char="b",move=turtle.back},
  {char="l",move=turtle.turnLeft},
  {char="r",move=turtle.turnRight},
  {char="u",move=turtle.up},
  {char="d",move=turtle.down}
}
while true do
  r = math.random(1, #moves)
  write(moves[r].char)
  moves[r].move()
end
aSdtIHN0dXBpZA== #22
Posted 27 October 2012 - 11:04 AM
SNIP

Okay, thanks a lot. I'll probably use that, and upgrade it when I see fit.

Just thought of adding a few options, and a main menu. I'll do that. Thanks again for all of your help, and everyone else's.