Skip to content

Instantly share code, notes, and snippets.

@adityasuseno
Last active January 11, 2023 02:36
Show Gist options
  • Save adityasuseno/eb33796f3ac1a899a76f88d9d52fcd51 to your computer and use it in GitHub Desktop.
Save adityasuseno/eb33796f3ac1a899a76f88d9d52fcd51 to your computer and use it in GitHub Desktop.
This Program Will Print a Desired Digit of Random 0 to 9 Positive Integer
use rand::Rng;
use std::io;
fn main() {
loop {
println!("How many number(s) do you want to print?");
let mut ans = String::new();
io::stdin()
.read_line(&mut ans)
.expect("Failed to read line");
let n: u32 = match ans.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
print_random_number(n);
break;
}
}
fn print_random_number(n: u32) {
for _ in 1..=n {
let mut rng = rand::thread_rng();
print!("{} ", rng.gen_range(0, 10));
}
println!("");
}
import java.util.Arrays;
import java.util.Random;
import java.io.*;
public class PrintRandomInteger {
public static void main(String[] args) throws IOException {
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many number(s) do you want to print: ");
int n = Integer.parseInt(bf.readLine());
if (n > 0) {
printRandomNumber(n);
}
else {
System.out.println("Do Not Enter Negative Value!");
}
}
catch(Exception NumberFormatException) {
System.out.println("Please Enter Only a Number!");
}
}
private static int[] getRandomNumber(int x) {
Random random = new Random();
int[] randomInt = new int[x];
for (int i = 0 ; i < x ; i++) {
randomInt[i] = random.nextInt(10);
}
return randomInt;
}
private static void printRandomNumber(int y) {
int[] randomInt = getRandomNumber(y);
System.out.print("Here is your random Number(s): ");
for (int i = 0 ; i < y ; i++) {
System.out.print(randomInt[i] + " ");
}
System.out.println("");
}
}
#!/usr/bin/env python3
from random import randrange
while True:
while True:
try:
n = int(input("Enter Desired Digit : ")) # ask how many digit do you want?
break
except:
print("Harap masukan angka saja!")
continue
a = [0 for n in range(n+1)] # Create a list for the size n
for _ in range(1, n+1): # fill our list with random 0~9 integer
a[_]=randrange(10)
for _ in range(1, n+1): # this functions print our list beautifully
print(a[_], end = ' ')
print('')
while True:
again = str(input("Lagi? (y/n) "))
if again == 'Y' or again == 'y':
break
elif again == 'N' or again == 'n':
break
else:
continue
if again == 'Y' or again == 'y':
continue
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment