Generate a random n bit binary string
A binary string is a sequence of 0s and 1s. To generate a random n bit binary string, we need to randomly select a value of either 0 or 1 for each bit position in the string. Here are some steps that we can follow to generate a random n bit binary string:
First, we need to decide on the value of n. This is the number of bits in the binary string that we want to generate.
We can use a random number generator to generate a sequence of n random numbers. Each random number should be either 0 or 1 with equal probability.
We can then convert the sequence of random numbers into a binary string. To do this, we can iterate through the sequence of random numbers and append either "0" or "1" to a new string for each number. For example, if the sequence of random numbers is [1, 0, 1, 1], we can convert this to the binary string "1011".
Finally, we can return the binary string as the output of our function.
Code Example
/*
C Program
Generate a random n bit binary string
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void randomBinaryText(int n)
{
if (n <= 0)
{
return;
}
// Use to collect result
char record[n + 1];
for (int i = 0; i < n; ++i)
{
// Collect the random number
record[i] = '0' + ((rand()) % 2);
}
record[n] = '\0';
// Display given number
printf("\n Given n : %d", n);
// Print the generated number
printf("\n Result : %s\n", record);
}
int main()
{
srand(time(NULL));
// Test
randomBinaryText(12);
randomBinaryText(8);
randomBinaryText(18);
return 0;
}
Output
Given n : 12
Result : 110100110100
Given n : 8
Result : 01010011
Given n : 18
Result : 111011011010100101
/*
Java Program
Generate a random n bit binary string
*/
import java.util.Random;
public class RandomNumber
{
public int absValue(int v)
{
if (v < 0)
{
return -v;
}
return v;
}
public void randomBinaryText(int n)
{
if (n <= 0)
{
return;
}
Random rand = new Random();
// Use to collect result
String result = "";
for (int i = 0; i < n; ++i)
{
// Collect the random number
result = (absValue(rand.nextInt() % 2)) + result;
}
// Display given number
System.out.print("\n Given n : " + n);
// Print the generated number
System.out.println("\n Result : " + result);
}
public static void main(String[] args)
{
RandomNumber task = new RandomNumber();
// Test
task.randomBinaryText(12);
task.randomBinaryText(8);
task.randomBinaryText(18);
}
}
Output
Given n : 12
Result : 100100000110
Given n : 8
Result : 10001001
Given n : 18
Result : 010000011110100001
// Include header file
#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
class RandomNumber
{
public: int absValue(int v)
{
if (v < 0)
{
return -v;
}
return v;
}
void randomBinaryText(int n)
{
if (n <= 0)
{
return;
}
// Use to collect result
string result = "";
for (int i = 0; i < n; ++i)
{
// Collect the random number
result = to_string(rand() % 2) + result;
}
// Display given number
cout << "\n Given n : " << n;
// Print the generated number
cout << "\n Result : " << result << endl;
}
};
int main()
{
srand(time(NULL));
RandomNumber *task = new RandomNumber();
// Test
task->randomBinaryText(12);
task->randomBinaryText(8);
task->randomBinaryText(18);
return 0;
}
Output
Given n : 12
Result : 001000000110
Given n : 8
Result : 10101011
Given n : 18
Result : 100000000010100101
// Include namespace system
using System;
public class RandomNumber
{
public int absValue(int v)
{
if (v < 0)
{
return -v;
}
return v;
}
public void randomBinaryText(int n)
{
if (n <= 0)
{
return;
}
Random rand = new Random();
// Use to collect result
String result = "";
for (int i = 0; i < n; ++i)
{
// Collect the random number
result = (this.absValue(rand.Next() % 2)) + result;
}
// Display given number
Console.Write("\n Given n : " + n);
// Print the generated number
Console.WriteLine("\n Result : " + result);
}
public static void Main(String[] args)
{
RandomNumber task = new RandomNumber();
// Test
task.randomBinaryText(12);
task.randomBinaryText(8);
task.randomBinaryText(18);
}
}
Output
Given n : 12
Result : 111110100000
Given n : 8
Result : 11001000
Given n : 18
Result : 011011100111000110
package main
import (
"fmt"
"math/rand"
"math"
"time"
"strconv"
)
/*
Go Program
Generate a random n bit binary string
*/
type RandomNumber struct {}
func getRandomNumber() * RandomNumber {
var me *RandomNumber = &RandomNumber {}
return me
}
func(this RandomNumber) absValue(v int) int {
if v < 0 {
return -v
}
return v
}
func(this RandomNumber) randomBinaryText(n int) {
if n <= 0 {
return
}
rand.Seed(time.Now().UnixNano())
// Use to collect result
var result string = ""
for i := 0 ; i < n ; i++ {
// Collect the random number
result = strconv.Itoa(rand.Intn(math.MaxInt64-1) % 2) + result
}
// Display given number
fmt.Print("\n Given n : ", n)
// Print the generated number
fmt.Println("\n Result : ", result)
}
func main() {
var task * RandomNumber = getRandomNumber()
// Test
task.randomBinaryText(12)
task.randomBinaryText(8)
task.randomBinaryText(18)
}
Output
Given n : 12
Result : 110011110001
Given n : 8
Result : 01010000
Given n : 18
Result : 111010111111011010
<?php
class RandomNumber
{
public function randomBinaryText($n)
{
if ($n <= 0)
{
return;
}
// Use to collect result
$result = "";
for ($i = 0; $i < $n; ++$i)
{
// Collect the random number
$result = strval(rand() % 2).$result;
}
// Display given number
echo("\n Given n : ".$n);
// Print the generated number
echo("\n Result : ".$result.
"\n");
}
}
function main()
{
$task = new RandomNumber();
// Test
$task->randomBinaryText(12);
$task->randomBinaryText(8);
$task->randomBinaryText(18);
}
main();
Output
Given n : 12
Result : 110010010001
Given n : 8
Result : 10001011
Given n : 18
Result : 101100100001111100
/*
Node JS Program
Generate a random n bit binary string
*/
class RandomNumber
{
randomBinaryText(n)
{
if (n <= 0)
{
return;
}
// Use to collect result
var result = "";
for (var i = 0; i < n; ++i)
{
// Collect the random number
result = ((Math.floor(Math.random() * (100)) + 2) % 2) + result;
}
// Display given number
process.stdout.write("\n Given n : " + n);
// Print the generated number
console.log("\n Result : " + result);
}
}
function main()
{
var task = new RandomNumber();
// Test
task.randomBinaryText(12);
task.randomBinaryText(8);
task.randomBinaryText(18);
}
main();
Output
Given n : 12
Result : 111001011111
Given n : 8
Result : 01100101
Given n : 18
Result : 110011010111100011
import random
import sys
# Python 3 Program
# Generate a random n bit binary string
class RandomNumber :
def randomBinaryText(self, n) :
if (n <= 0) :
return
# Use to collect result
result = ""
i = 0
while (i < n) :
# Collect the random number
result = str(random.randint(0,sys.maxsize-2)%2) + result
i += 1
# Display given number
print("\n Given n : ", n, end = "")
# Print the generated number
print("\n Result : ", result)
def main() :
task = RandomNumber()
# Test
task.randomBinaryText(12)
task.randomBinaryText(8)
task.randomBinaryText(18)
if __name__ == "__main__": main()
Output
Given n : 12
Result : 101011001110
Given n : 8
Result : 01001011
Given n : 18
Result : 101101000100111111
# Ruby Program
# Generate a random n bit binary string
class RandomNumber
def randomBinaryText(n)
if (n <= 0)
return
end
r = Random.new
max_int = (2**(0.size * 8 -2) -1)
# Use to collect result
result = ""
i = 0
while (i < n)
# Collect the random number
result = (r.rand(0...max_int) % 2).to_s + result
i += 1
end
# Display given number
print("\n Given n : ", n)
# Print the generated number
print("\n Result : ", result, "\n")
end
end
def main()
task = RandomNumber.new()
# Test
task.randomBinaryText(12)
task.randomBinaryText(8)
task.randomBinaryText(18)
end
main()
Output
Given n : 12
Result : 111101111010
Given n : 8
Result : 10011100
Given n : 18
Result : 100100010000111011
/*
Scala Program
Generate a random n bit binary string
*/
class RandomNumber()
{
def absValue(v: Int): Int = {
if (v < 0)
{
return -v;
}
return v;
}
def randomBinaryText(n: Int): Unit = {
if (n <= 0)
{
return;
}
val r = new scala.util.Random;
// Use to collect result
var result: String = "";
var i: Int = 0;
while (i < n)
{
// Collect the random number
result = (absValue(r.nextInt() % 2)).toString() + result;
i += 1;
}
// Display given number
print("\n Given n : " + n);
// Print the generated number
println("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: RandomNumber = new RandomNumber();
// Test
task.randomBinaryText(12);
task.randomBinaryText(8);
task.randomBinaryText(18);
}
}
Output
Given n : 12
Result : 111101010000
Given n : 8
Result : 11011101
Given n : 18
Result : 011100011110001000
import Foundation
#if os(Linux)
srandom(UInt32(time(nil)))
#endif
/*
Swift 4 Program
Generate a random n bit binary string
*/
class RandomNumber
{
func absValue(_ v: Int) -> Int
{
if (v < 0)
{
return -v;
}
return v;
}
func randomBinaryText(_ n: Int)
{
if (n <= 0)
{
return;
}
var number = 0;
let max = UInt8.max-1 ;
// Use to collect result
var result: String = "";
var i: Int = 0;
while (i < n)
{
//Get new rand number
#if os(Linux)
number = Int(random())
#else
number = Int(arc4random_uniform(UInt32(max)))
#endif
// Collect the random number
result = String(number%2) + result;
i += 1;
}
// Display given number
print("\n Given n : ", n, terminator: "");
// Print the generated number
print("\n Result : ", result);
}
}
func main()
{
let task: RandomNumber = RandomNumber();
// Test
task.randomBinaryText(12);
task.randomBinaryText(8);
task.randomBinaryText(18);
}
main();
Output
Given n : 12
Result : 100001111111
Given n : 8
Result : 10100110
Given n : 18
Result : 101111000000010110
/*
Kotlin Program
Generate a random n bit binary string
*/
import kotlin.random.Random
class RandomNumber
{
fun absValue(v: Int): Int
{
if (v < 0)
{
return -v;
}
return v;
}
fun randomBinaryText(n: Int): Unit
{
if (n <= 0)
{
return;
}
// Use to collect result
var result: String = "";
var i: Int = 0;
while (i < n)
{
// Collect the random number
result = (absValue(Random(
System.nanoTime()).nextInt()) % 2
).toString() + result;
i += 1;
}
// Display given number
print("\n Given n : " + n);
// Print the generated number
println("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: RandomNumber = RandomNumber();
// Test
task.randomBinaryText(12);
task.randomBinaryText(8);
task.randomBinaryText(18);
}
Output
Given n : 12
Result : 101001110011
Given n : 8
Result : 01010001
Given n : 18
Result : 100001111100001110
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