My First Script (Assistance Needed)

I created a script that will randomize the alphabet.

When I run it, it seems to stall instead of run. Sometimes it works, other times stall.

Is there another way to accomplish this?

Spoiler
def create_code
complete = 0
code_pos = 61

while(complete < 27)
rand_value = rand(27) + 1

if $game_switches[rand_value] == false
$game_switches[rand_value] = true
$game_switches[26 + rand_value] = true
$game_variables[code_pos] = rand_value

code_pos += 1
complete += 1
end
end
end

Update: I removed the complete variable and changed the while condition and it is acting better.
If I were to randomize the alphabet, I'd do it a bit differently. Definitely not using switches and variables (except for the final storage).
This is how I'd do it in javascript:
Code:
var alph = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < 70; i++) {
var letter = alph[Math.floor(Math.random() * 26)];
alph = alph.replace(letter, "");
alph += letter;
}
$gameVariables.setValue(1, alph);

This would take a random letter out of the alphabet string and put it at the end 70 times. The 70 is just ensuring it will get enough shuffles to be randomized. It can be increased, for example for a deck shuffle I made it 150 to give it a thorough shuffle.
If I were to randomize the alphabet, I'd do it a bit differently. Definitely not using switches and variables (except for the final storage).
This is how I'd do it in javascript:
Code:
var alph = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < 70; i++) {
var letter = alph[Math.floor(Math.random() * 26)];
alph = alph.replace(letter, "");
alph += letter;
}
$gameVariables.setValue(1, alph);

This would take a random letter out of the alphabet string and put it at the end 70 times. The 70 is just ensuring it will get enough shuffles to be randomized. It can be increased, for example for a deck shuffle I made it 150 to give it a thorough shuffle.

Thank you poryg, that will come in nicely when I port this over to MV, but for now I am still using VX Ace and learning Ruby.


This code seems to work quite nicely, although it still seems to have some gremlins when displayed on screen.

I used hash symbols to get the code neatly displayed. I also commented to make things (hopefully) easier to understand.

Spoiler
#def create_code
#
# Code variables are from 61 to 86 (A-Z)
#
# code_pos = 61
#
# Loop until all letters are used
#
# while(code_pos != 87)
#
# Get a random value between 1 and 26
#
# rand_value = rand(26) + 1
#
# if the position is empty then fill it
#
# if $game_switches[rand_value] == false
#
# Set both letter switches to true because they have been chosen
# Set the letter value in variable [code_pos] to the new letter value
#
# $game_switches[rand_value] = true
# $game_switches[26 + rand_value] = true
# $game_variables[code_pos] = rand_value
#
# Increase the code position until it has gone through all 26 positions
#
# code_pos += 1
# end
# end
#end

^ Okay, that didn't do as expected ^

Edit: random value was one value off. Should be 26 NOT 27. Rookie mistake.
Code:
alph = "abcdefghijklmnopqrstuvwxyz"
tmpa = alph.dup
rnda = ""
loop
  rnda.concat (tmpa.slice!(rand(tmpa.size).round, 1))
until tmpa.size < 1
I think?
Couldn't you just do:
Code:
alph = ('a'..'z').to_a
new_alph = alph.shuffle
and then
Code:
string = new_alph.join
for the string version?
Code:
alph = "abcdefghijklmnopqrstuvwxyz"
tmpa = alph.dup
rnda = ""
loop
  rnda.concat (tmpa.slice!(rand(tmpa.size).round, 1))
until tmpa.size < 1
I think?

Thank you @gstv87

Couldn't you just do:
Code:
alph = ('a'..'z').to_a
new_alph = alph.shuffle
and then
Code:
string = new_alph.join
for the string version?

Thank you @A-Moonless-Night. This is quite tiny.

I will check out these snippets and see how they do. I am new to Ruby and have a LOT to learn.
I'll try explain myself:

Code:
alph = ('a'..'z').to_a
This makes a local variable, alph, and sets it to be a new array of the letters a–z. It's just a neater way of doing this:
Code:
alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] ... etc.

Code:
new_alph = alph.shuffle
This makes another local variable, new_alph, and sets it to be a shuffled version of alph. The method 'shuffle' is for arrays and basically shuffles all the elements in the array around randomly. The reason we make a new local variable is so we can leave the original local variable untouched. However, if you did want to permanently change the original local variable, you could do:
Code:
alph.shuffle!

I wasn't too sure of the purpose of having this randomised alphabet and whether you needed it to remain an array or become a string.
Code:
string = new_alph.join
This makes another local variable, string, and sets it to be a string version of the randomised alphabet array (new_alph). The method 'join' is for arrays and it takes each element of the array, converts it to a string and then joins them all together. If you wanted there to be a separator in between each letter, you can do this:
Code:
string = new_alph.join(x)
where x is whatever you want, so long as it's a string. You could do "-" or ", " or "." or " to the ", for example.


Also, pro tip: if you want to have your code displayed nicely, use the forum code tag.
Code:
[CODE] code goes here [ /CODE] without the space before /
Thank you A-Moonless-Night.

Here is the new code:

Code:
  def create_code
    code = ('A'..'Z').to_a
    code = code.shuffle.join

    code.split('').each_with_index do |letter, i|
      $game_variables[61 + i] = Words::Letters.index(letter)
    end
  end

So much smaller and probably better than the original.

Testing the output all works out as expected.
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/my-first-script-assistance-needed.102518/