A Method of Selecting Random Characters from Given String

a method of selecting random characters from given string

$str = 'helloworld';

$randomChar = $str[rand(0, strlen($str)-1)];

CodePad.

selecting a random char in a string in java with a certain method

public static char selectAChar(String s){

Random random = new Random();
int index = random.nextInt(s.length());
return s.charAt(index);

}

Need to Select a random letter in a string and change it

First, convert the string into a character array. Then, generate a random integer between 0 and the length of the string. Set the character at that array index to an underscore. Return a new string with the modified character array.

String changeString(String s)
{
char[] characters = s.toCharArray();
int rand = (int)(Math.random() * s.length());
characters[rand] = '_';
return new String(characters);
}

Picking a random letter in a string

You can calculate 3 numbers and then get 3 random characters by charAt() and then concatenate them in a string.

You could implement something like this:

Scanner keyboard = new Scanner(System.in);
String inp = keyboard.nextLine();
Random generator = new Random();
String newString = ""; //contains the extracted letters
int randomPositionOfLetter;
for(int i=1;i<=3;i++){
// calculating a random position of a char in the string
randomPositionOfLetter = generator.nextInt(inp.length());
newString = newString + inp.charAt(randomPositionOfLetter);
}

You could also modify the code to not be able to randomly choose the same number more than one time.

Generate random string/characters in JavaScript

I think this will work for you:

function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

console.log(makeid(5));

How to randomly select letters in Java?

What about something like this?

Random rnd = new Random();
String characters = "ABCDEFGH";
char randomChar = characters.charAt(rnd.nextInt(characters.length()));

Random character String processing

char (int(random(65, 65+24))); is indeed the right way to get a random letter.

character += char (int(random(65, 65+24))); means you append/concatenate one letter at a time therefore your character variable will increase with a new char each iteration.

character = char (int(random(65, 65+24))); would replace the current char with a new random each iteration.

If you want to make vertical text you can use the new line character (\n).
Unfortunately you can't easily swap a character out with the String class, but with a bit of substring() and concatenation you can simulate something similar. (The StringBuilder java class would make character swapping easier). Here's a commented example using String:

// full string of letters
String letters = "";
// maximum letters in a string
int maxLetters = 12;
// which character to swap
int charIndex = 0;

void setup(){
size(300, 300);
fill(0, 192, 0);
textAlign(CENTER);
textFont(createFont("Courier New", 12), 12);
// populate the string
for(int i = 0 ; i < maxLetters; i++){
letters += getRandomLetter() + "\n";
}
}

void draw(){
// pick random char
char randomChar = getRandomLetter();
// replace existing characters
letters = setCharAt(letters, randomChar, charIndex);
// increment the char index by 2 to include \n
// use the modulo operator to loop back to 0
charIndex = (charIndex + 2) % letters.length();

// render the text
background(0);
text(letters, width * 0.5, height * 0.25);
}

// returns a random a-z char
char getRandomLetter(){
return char (int(random(65, 65+24)));
}

// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex){
return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
}

Update The above can be encapuslated for re-use:

MText text = new MText();

void setup(){
size(300, 300);
fill(0, 192, 0);
textAlign(CENTER);
textFont(createFont("Courier New", 12), 12);
}

void draw(){
background(0);
text.draw();
}

class MText{
// full string of letters
String letters = "";
// maximum letters in a string
int maxLetters = 12;
// which character to swap
int charIndex = 0;

float x, y;

MText(){
// populate the string
for(int i = 0 ; i < maxLetters; i++){
letters += getRandomLetter() + "\n";
}
// default position
x = width * 0.5;
y = height * 0.25;
}

void draw(){
// pick random char
char randomChar = getRandomLetter();
// replace existing characters
letters = setCharAt(letters, randomChar, charIndex);
// increment the char index by 2 to include \n
// use the modulo operator to loop back to 0
charIndex = (charIndex + 2) % letters.length();
// render text
text(letters, x, y);
}

// returns a random a-z char
char getRandomLetter(){
return char (int(random(65, 65+24)));
}

// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex){
return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
}

}

The advantage of grouping/encapuslating the functionality in a class is that mulitple instances can be easily managed:

int numTexts = 60;
ArrayList<MText> texts = new ArrayList<MText>();

void setup(){
size(300, 300);
fill(0, 192, 0);
textAlign(CENTER);
textFont(createFont("Courier New", 12), 12);
for(int i = 0 ; i < numTexts; i++){
MText text = new MText();
text.x = random(width);
text.y = random(height);
texts.add(text);
}
}

void draw(){
background(0);
for(MText text: texts) text.draw();
}

class MText{
// full string of letters
String letters = "";
// maximum letters in a string
int maxLetters = 12;
// which character to swap
int charIndex = 0;

float x, y;
float vy;
float textHeight;

MText(){
// populate the string
for(int i = 0 ; i < maxLetters; i++){
letters += getRandomLetter() + "\n";
}
// default position
x = width * 0.5;
y = height * 0.25;
// default Y velocity
vy = random(.16018, 2.1);
textHeight = (textAscent() - textDescent()) * maxLetters;
}

void draw(){
// pick random char
char randomChar = getRandomLetter();
// replace existing characters
letters = setCharAt(letters, randomChar, charIndex);
// increment the char index by 2 to include \n
// use the modulo operator to loop back to 0
charIndex = (charIndex + 2) % letters.length();
// update position
y += vy;
if(y > height + textHeight){
y = -textHeight * 2;
vy = random(.16018, 2.1);
}
// render text
fill(0, 192, 0);
text(letters, x, y);
text(0, 255, 0);
text(letters.charAt(0), x, y);
}

// returns a random a-z char
char getRandomLetter(){
return char (int(random(65, 65+24)));
}

// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex){
return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
}

}

Hopefully the above is a useful direction. There many ways of achieving a similar result. In terms of improvements, little touches such as a single brighter green character (maybe even making it glow), aligning x text so it doesn't overlap for the matrix 1 effect, fading/leaving trails and chaning camera z position for the matrix 2 effect, etc.

Update 2
The updated code didn't include the part instantiatting Letter and rendering it. The error I encountered was due to the fact that string was initialized to be empty (String character = "";) and the random letter function couldn't replace a character at an index that didn't exist (yet). The solution in this case would be to intialize the string with a few characters first.
For example moving:

for (int i = 0; i < maxLetters; i++) {
character +=getRandomLetter() +"\n";
}

in the constructor before pickLetter() gets called.
Full example:

Letter l = new Letter(150, 15, 1.5);

void setup(){
size(300, 300);
}

void draw(){
background(0);
l.display();
}

class Letter{
PVector pos;
float speed;
String letter;
float change_threshold = 0.1;
color cor = color (3, 160, 98);
String character = "";
// maximum letters in a string
int maxLetters = 35;
// which character to swap
int charIndex = 0;

Letter (float xpos, float ypos, float vel) {
for (int i = 0; i < maxLetters; i++) {
character +=getRandomLetter() +"\n";
}
this.pickLetter();
pos = new PVector (xpos, ypos);
speed = vel;
}

void display() {

fill(this.cor);
text(this.letter, this.pos.x, this.pos.y);
float p = random(1);
if (p < this.change_threshold && this.cor != color(255)) {
this.pickLetter();
}
}

void pickLetter() {

//String character = str (floor (random(10)));
//String character = new String ("a");
//character += char (int(random(65, 65+24)));
char randomChar = getRandomLetter();
character = setCharAt(character, randomChar, charIndex);
charIndex = (charIndex + 2)%character.length();
this.letter = character;
}

void fall() {
this.pos.y += this.speed;
}

// returns a random a-z char
char getRandomLetter() {
return char (int(random(65, 65+24)));
}

// return a new string with a char swapped at the given index
String setCharAt(String myString, char myNewChar, int myCharIndex) {
return myString.substring(0, myCharIndex) + myNewChar + myString.substring(myCharIndex + 1);
}
}

How can I select random characters in a pythonic way?

The usual way is random.choice()

>>> import string
>>> import random
>>> random.choice(string.ascii_letters + string.digits)
'v'


Related Topics



Leave a reply



Submit