Vala. Generate Random Character

This Vala snippet shows how to generate a random character (char).

Function char_random

The function char_random returns a randomly generated character (char). By default the returned character will be in the ranges a-z and A-Z. However, the function allows you to provide your own characters as string, which will be used to randomly pick a character from.

//======================== START OF FUNCTION ==========================//
// FUNCTION: char_random                                             //
char char_random(string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"){
	int random_index = Random.int_range(0,charset.length);
	char* chars = (char*)charset;
	char ch = chars[random_index];
	return ch;
// FUNCTION: char_random                                               //
//========================= END OF FUNCTION ===========================//


// Generate random char in default range 
char rchar = char_random();

// Random small case char 
char rchar_smallcap = char_random("abcdefghijklmnopqrstuvwxyz");

// Random upper case char 
char rchar_uppercap = char_random("ABCDEFGHIJKLMNOPQRSTUVWXYZ");