• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

A better way to get a random letter over choose("A" through "E")

Bentley

Member
Hello. I want to make a typing game and I have a quick question.

1. I'm going to use random letters to make a string. These are my first thoughts on how to do it:

Code:
letter_total = 10;

for (var i = 0; i < letter_total; i += 1)
{
    letter[i] = choose("A", "B", "C"); // ...all the way to "E"
}
In the above example, each word would be 10 letters long, and each letter is randomized. Is there
an easier way to randomize the letter rather than choose("A"..."E")

Thanks for reading!
 
Last edited:

jo-thijs

Member
Hello. I want to make a typing game and I have a quick question.

1. I'm going to use random letters to make a string. These are my first thoughts on how to do it:

Code:
letter_total = 10;

for (var i = 0; i < letter_total; i += 1)
{
    letter[i] = choose("A", "B", "C"); // ...all the way to "E"
}
In the above example, each word would be 10 letters long, and each letter is randomized. Is there
an easier way to randomize the letter rather than choose("A"..."E")

Thanks for reading!
There is.

If you want any random capital letter, you can just use this:
Code:
chr(irandom_range(ord('A'), ord('Z')))
 
Last edited:
H

Homunculus

Guest
You can also do this:

Code:
var letters = "ABCDE";
var rand = irandom_range(1,string_length(letters));
var rand_letter = string_char_at(letters,rand);
 

Bentley

Member
There is.

If you want any random capital letter, you can just use this:
Code:
chr(irandom_range(ord('A'), ord('Z')))
Wow, so that returns a random letter from A to Z?

Edit: Thanks jo-thijs, answered my question perfectly : )
 
Last edited:
E

elementbound

Guest
Yeah, it does indeed. You have to convert that integer to a character with the chr function:
Code:
letter[i] = chr(irandom_range(ord("A"), ord("Z")));
 
Top