Skip to main content

Random character selection from string

Here given code implementation process.

// Include header file
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;
/*
    C++ program
    Random character selection from string
*/
class Selection
{
	public:
		// Returns a random character
		char randomSelection(string text)
		{
			int n = text.length();
			int position = rand() % n;
			return text[position];
		}
	void password(string text, int n)
	{
		string result = "";
		for (int i = 0; i < n; ++i)
		{
			result += this->randomSelection(text);
		}
		// Display given text
		cout << "\n Given size : " << n;
		// Display calculated result
		cout << "\n Result     : " << result;
	}
};
int main()
{
  	srand(time(NULL));
	Selection *task = new Selection();
	string text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
	// n = 30
	task->password(text, 30);
	// n = 15
	task->password(text, 15);
	// n = 8
	task->password(text, 8);
	return 0;
}

input

 Given size : 30
 Result     : acpXf9p4ceab7sSY1RnP1h@b9%uoTQ
 Given size : 15
 Result     : Wtq7CH3q9VGYuCC
 Given size : 8
 Result     : 7Z2N9GDd
import java.util.Random;
/*
    Java program
    Random character selection from string
*/
public class Selection
{
	// Returns a random character
	public char randomSelection(String text)
	{
		int n = text.length();
		int position = (int)(Math.random() *(n));
		return text.charAt(position);
	}
	public void password(String text, int n)
	{
		String result = "";
		for (int i = 0; i < n; ++i)
		{
			result += randomSelection(text);
		}
		// Display given text
		System.out.print("\n Given size : " + n);
		// Display calculated result
		System.out.print("\n Result     : " + result);
	}
	public static void main(String[] args)
	{
		Selection task = new Selection();
		String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
		// n = 30
		task.password(text, 30);
		// n = 15
		task.password(text, 15);
		// n = 8
		task.password(text, 8);
	}
}

input

 Given size : 30
 Result     : BtdKDY9cmVivxFkhZH@H%1F9THW%MD
 Given size : 15
 Result     : WugLbSrX1kDH8YH
 Given size : 8
 Result     : aXkac$zU
// Include namespace system
using System;
/*
    Csharp program
    Random character selection from string
*/
public class Selection
{
	// Returns a random character
	public char randomSelection(String text)
	{
		int n = text.Length;
      	Random rand = new Random();
		int position = rand.Next(0, n);;
		return text[position];
	}
	public void password(String text, int n)
	{
		String result = "";
		for (int i = 0; i < n; ++i)
		{
			result += this.randomSelection(text);
		}
		// Display given text
		Console.Write("\n Given size : " + n);
		// Display calculated result
		Console.Write("\n Result     : " + result);
	}
	public static void Main(String[] args)
	{
		Selection task = new Selection();
		String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
		// n = 30
		task.password(text, 30);
		// n = 15
		task.password(text, 15);
		// n = 8
		task.password(text, 8);
	}
}

input

 Given size : 30
 Result     : qzaw8PzBA9p%y$uDPJWVrC%otAJRtp
 Given size : 15
 Result     : sc6L5o$gVgsO2X7
 Given size : 8
 Result     : kjuFun2$
package main
import (
    "fmt"
    "math/rand"
)
/*
    Go program
    Random character selection from string
*/

// Returns a random character
func  randomSelection(text string) byte {
	var n int = len(text) - 1
	var position int = rand.Intn(n);
	return text[position]
}
func password(text string, n int) {
	var result string = ""
	for i := 0 ; i < n ; i++ {
		result += string(randomSelection(text))
	}
	// Display given text
	fmt.Print("\n Given size : ", n)
	// Display calculated result
	fmt.Print("\n Result     : ", result)
}
func main() {

	var text string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890"
	// n = 30
	password(text, 30)
	// n = 15
	password(text, 15)
	// n = 8
	password(text, 8)
}

input

 Given size : 30
 Result     : kvlOz3oAVP91@JwJutHptphN7H7X%F
 Given size : 15
 Result     : QN2pYfWfopXoESS
 Given size : 8
 Result     : RSJxclQn
<?php
/*
    Php program
    Random character selection from string
*/
class Selection
{
	// Returns a random character
	public	function randomSelection($text)
	{
		$n = strlen($text);
		$position = rand(0, $n-1);
		return $text[$position];
	}
	public	function password($text, $n)
	{
		$result = "";
		for ($i = 0; $i < $n; ++$i)
		{
			$result .= $this->randomSelection($text);
		}
		// Display given text
		echo("\n Given size : ".$n);
		// Display calculated result
		echo("\n Result     : ".$result);
	}
}

function main()
{
	$task = new Selection();
	$text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
	// n = 30
	$task->password($text, 30);
	// n = 15
	$task->password($text, 15);
	// n = 8
	$task->password($text, 8);
}
main();

input

 Given size : 30
 Result     : #@%a5gS#wsqdQS%XoL%LXi$NW3qX$j
 Given size : 15
 Result     : jpVZCQ3UDp0uEQ9
 Given size : 8
 Result     : 5nnGdzd8
/*
    Node JS program
    Random character selection from string
*/
class Selection
{
	// Returns a random character
	randomSelection(text)
	{
		var n = text.length;
		var position = Math.floor(Math.random() * n );
		return text.charAt(position);
	}
	password(text, n)
	{
		var result = "";
		for (var i = 0; i < n; ++i)
		{
			result += this.randomSelection(text);
		}
		// Display given text
		process.stdout.write("\n Given size : " + n);
		// Display calculated result
		process.stdout.write("\n Result     : " + result);
	}
}

function main()
{
	var task = new Selection();
	var text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
	// n = 30
	task.password(text, 30);
	// n = 15
	task.password(text, 15);
	// n = 8
	task.password(text, 8);
}
main();

input

 Given size : 30
 Result     : p1k2F90IDUmCt#7uvbifAta9jbw5Rn
 Given size : 15
 Result     : kUDlsEMyayko#wm
 Given size : 8
 Result     : 7A7$9DhO
import random
import sys
#    Python 3 program
#    Random character selection from string
class Selection :
	#  Returns a random character
	def randomSelection(self, text) :
		n = len(text)
		position = random.randint(0,n-1)
		return text[position]
	
	def password(self, text, n) :
		result = ""
		i = 0
		while (i < n) :
			result += self.randomSelection(text)
			i += 1
		
		#  Display given text
		print("\n Given size : ", n, end = "")
		#  Display calculated result
		print("\n Result     : ", result, end = "")
	

def main() :
	task = Selection()
	text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890"
	#  n = 30
	task.password(text, 30)
	#  n = 15
	task.password(text, 15)
	#  n = 8
	task.password(text, 8)

if __name__ == "__main__": main()

input

 Given size :  30
 Result     :  %1jc6x$tCLq6C3J9ybmRwGUGY7Uc4a
 Given size :  15
 Result     :  Vtn%Q5GvWEY3qv9
 Given size :  8
 Result     :  NtFpWH$u
#    Ruby program
#    Random character selection from string
class Selection 
	#  Returns a random character
	def randomSelection(text) 
		n = text.length 
		r = Random.new
		position = r.rand(0...n)
		return text[position]
	end

	def password(text, n) 
		result = ""
		i = 0
		while (i < n) 
			result += self.randomSelection(text)
			i += 1
		end

		#  Display given text
		print("\n Given size : ", n)
		#  Display calculated result
		print("\n Result     : ", result)
	end

end

def main() 
	task = Selection.new()
	text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890"
	#  n = 30
	task.password(text, 30)
	#  n = 15
	task.password(text, 15)
	#  n = 8
	task.password(text, 8)
end

main()

input

 Given size : 30
 Result     : 3s@STEZ#LusqL8ipF6HGFMvcSaOLhM
 Given size : 15
 Result     : H$o9JKuDYyMvcx8
 Given size : 8
 Result     : 1XzFF9Z2
import scala.collection.mutable._;
/*
    Scala program
    Random character selection from string
*/
class Selection()
{
	// Returns a random character
	def randomSelection(text: String): Char = {
		var n: Int = text.length();
  		val r = new scala.util.Random
		var position: Int = r.nextInt(n-1) ;
		return text.charAt(position);
	}
	def password(text: String, n: Int): Unit = {
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			result += randomSelection(text);
			i += 1;
		}
		// Display given text
		print("\n Given size : " + n);
		// Display calculated result
		print("\n Result     : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Selection = new Selection();
		var text: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
		// n = 30
		task.password(text, 30);
		// n = 15
		task.password(text, 15);
		// n = 8
		task.password(text, 8);
	}
}

input

 Given size : 30
 Result     : %$2t%RAwtRWC@U7XGYOGB8ThnTDtd%
 Given size : 15
 Result     : I#sgkDcddoJKoy$
 Given size : 8
 Result     : zLGYi8tV
import Foundation

#if os(Linux)
    srandom(UInt32(time(nil)))
#endif
/*
    Swift 4 program
    Random character selection from string
*/
class Selection
{
	// Returns a random character
	func randomSelection(_ text: [Character]) -> Character
	{	
		let n: Int = text.count;
      	var position: Int = 0;
        #if os(Linux)
        position = Int(random()%(n)) 
        #else
        position = Int(arc4random_uniform(n)) 
        #endif
		return text[position];
	}
	func password(_ text: String, _ n: Int)
	{
		var result: String = "";
      	let data = Array(text);
		var i: Int = 0;
		while (i < n)
		{
			result += String(self.randomSelection(data));
			i += 1;
		}
		// Display given text
		print("\n Given size : ", n, terminator: "");
		// Display calculated result
		print("\n Result     : ", result, terminator: "");
	}
}
func main()
{
	let task: Selection = Selection();
	let text: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
	// n = 30
	task.password(text, 30);
	// n = 15
	task.password(text, 15);
	// n = 8
	task.password(text, 8);
}
main();

input

 Given size :  30
 Result     :  oj8s@5FT7lBKwIzpMXwfrbUo2SDPoM
 Given size :  15
 Result     :  @Ntv2dp6wkevuM1
 Given size :  8
 Result     :  d@Cyifb7
/*
    Kotlin program
    Random character selection from string
*/
class Selection
{
	// Returns a random character
	fun randomSelection(text: String): Char
	{
		val n: Int = text.length;
		val position: Int = kotlin.random.Random.nextInt(0, n);
		return text.get(position);
	}
	fun password(text: String, n: Int): Unit
	{
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			result += this.randomSelection(text);
			i += 1;
		}
		// Display given text
		print("\n Given size : " + n);
		// Display calculated result
		print("\n Result     : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Selection = Selection();
	val text: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#$%1234567890";
	// n = 30
	task.password(text, 30);
	// n = 15
	task.password(text, 15);
	// n = 8
	task.password(text, 8);
}

input

 Given size : 30
 Result     : gIbOzJjCTd5BREqQ8lpqI5GHTbP7zh
 Given size : 15
 Result     : HbMwXJOA0lBBcWx
 Given size : 8
 Result     : no3c7zg7




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment