Skip to main content

Outcomes of a Dice || Generating Random numbers in Java



import java.util.*;
class GenerateRandomNumber
{
public static void main(String args[])
{
int i=0;
Random r=new Random();
Scanner sc=new Scanner(System.in);

System.out.println("How many random numbers ,do you want to generate?");
int value=sc.nextInt();
System.out.println();
System.out.println("Generating Numbers");
for(i=0;i<value;i++)
{
int n=r.nextInt(6) + 1;
System.out.println(n);
}
}
}


/*In the loop it is written:
int n=r.nextInt(6) + 1;
Here 6 inside the parentesis is the maximum value of random number,since here we are considering a dice so I have set that to 6. And the +1 indicates the minimum possible number generated.You have to include
import java.util.*; library to use Random(). */


Comments

Popular posts from this blog

Find number of vowels in a string.

#include<stdio.h> int main() { char str[25]; printf("Enter string.\n"); gets(str); int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') { count++; } i++; } printf("\nNo. of vowels is %d",count); return 0; }

Java program on two dimension Matrix of String or Characters.

import java.util.*; class CharArray { public static void main(String args[]) { String a[][]; a=new String[3][3]; int i=0,j=0; Scanner sc=new Scanner(System.in); System.out.println("Enter your characters"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { a[i][j]=sc.next(); } } System.out.println(); for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } } /* Here I have used 3*3 matrix, you can also form any multi-dimension matrix by providing its size using scanner.*/