ID:139161
 
Code:
var/newkey = ""//this is for the new betakey setup
verb
AddKey()//randomizes a key for you to use
var/num = input("How many keys do you want? Max of 100")as num//how many keys do you want?
if(num>>100)
num=100
else if(num <= 0)
num = 1
while(num)
RandomBetaKey()
betakeylist+=newkey
proc
RandomBetaKey()//the process for making a new betakey

var/html = ""
var/n = rand(1,26)//randomize n
var/char = ""
loop//set a link for a looping later
spawn()
switch(n)//randomize n again
if(1)//if n == 1 the char var will be A
char = "A"
else if (2)//repeat for each letter of alphabet, increasing n by 1
char = "B"
else if (3)
char = "C"
else if (4)
char = "D"
else if (5)
char = "E"
else if (6)
char = "F"
else if (7)
char = "G"
else if (8)
char = "H"
else if (9)
char = "I"
else if (10)
char = "J"
else if (11)
char = "K"
else if (12)
char = "L"
else if (13)
char = "M"
else if (14)
char = "N"
else if (15)
char = "O"
else if (16)
char = "P"
else if (17)
char = "Q"
else if (18)
char = "R"
else if (19)
char = "S"
else if (20)
char = "T"
else if (21)
char = "U"
else if (22)
char = "V"
else if (23)
char = "W"
else if (24)
char = "X"
else if (25)
char = "Y"
else if (26)
char = "Z"
if(length(html)==4)//after 4 chars
html+="-"//add a -
goto loop//loop this
else if(length(html)==9)//after the second set of 4
html+="-"//add -
goto loop//loop
else if(length(html)==14)//after the third set
html+="-"
goto loop
else if(length(html)==19)//after the fourth set
html+="-"
else if(length(html)>=23)//when we finish
goto end
else//if the length is not specified and we haven't ended yet
html+=char//add the char to html
goto loop//loop
end//link for end
newkey = html//make the variable newkey as html (so we can have the verb make the new key and such)


Problem description:
When I run the AddKey Verb, it literally crashes the world. The window turns non responding, I waited 15 minutes but it never finished, and I only tried making one (1) key.
The reason for that happening is due to the while(num) in AddKey() is causing an infinite loop (because you did not add 'break' nor decrease num's value to 0 which can break the loop).

In your RandomBetaKey():
  1. To make the procedure more efficient, use while() instead of label... go to label... you can break out of the loop by using break. It's probably best if you did while(length(html) < 23) so that the loop breaks when html is >= 23 in length.
  2. You are using switch() incorrectly. For all options, you must use if(), once 'else' appears (including in 'else if') - it breaks out of the switch(). This means you are saying:
    switch(n)
    if(1)
    char = "A"
    else
    if(2) // Always true since it is no longer considered to be part of the switch()
    char = "B"
    ... etc... which doesn't matter since B will always be true
    The way you have it now will result in a 1/26 chance for A and 25/26 chance for B with no chance for all the other letters.

    The correct method would be:
    switch(n)
    if(1)
    char = "A"
    if(2)
    char = "B"
    if(3)
    char = "C"
    ...etc...
    else
    None of the if() above happened and this was called
    if(X)
    This will never be checked as it happens after the 'else', which is like a break for switch()
  3. For more efficiency, you can use ascii2text(100+n+((n-(n%8))/4)) or ascii2text(100+n+(n<8)0?:(n<16)?2:(n<24)?4:6) to produce A-Z instead of one giant switch(). This is due to the ASCII result for 101 is A, 102 is B ... and 132 is Z. Note that 1X8 & 1X9 are other characters
    http://www.asciitable.com/
In response to GhostAnime
Thank you!

I fixed the while(num) loop and added 'num--' to the end of it. It does now output a key (after I fixed the RandomBetaKey() process). I would never have thought of using the ascii2text() process, I would have kept switching for the characters. Again, thank you! I now successfully have it output a key using your help. If only BYOND had a rep system.
In response to Fluidfire
You're welcome. One thing I forgot to point out is the >> in your first if() statement. It will not give you the effect of > as the >> would be a bit shift operator.

You can replace
      if(num>100)
num=100
else if(num <= 0)
num = 1
with
num = max(1,min(100, num))
to limit the range to 1 - 100 (max() picks the highest number so that the lowest possible # is 0... ex: max(3, 0) = 3, max(-4, 0) = 0. Same idea with min() and 100)
In response to GhostAnime
Thats a rather handy tip, thank you, must be more effective than the 'if' method as well. I have much left to learn about DM. I must go find another neat idea, mess around with it, break it, and ask for help again later ._.