Displaying 500 random integers [duplicate] - java

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
I want to generate 500 random numbers and display them next to their indexes. I want the numbers to be 1 though 10. But when I run my code it only generates 13 random numbers and the numbers are greater than 10. Please help.
public class RandomStats {
public static void main(String[] args) {
int [] stats = new int [501];
int numRolls;
int outcome;
for (int i = 0; i <=500; i++ ){
outcome = (int) ( 10 * Math.random() + 1)
+ (int) ( 10 * Math.random () + 1);
stats[outcome] += 1;
}
for ( int i = 2; i <=500; i++) {
System.out.println(i + ": " + stats[i]);
}
}
}

While saving in the array, you are doing stats[outcome] += 1;, where your array is updated with same index again and again and thats the reason you only see ~13 values and rest as 0. I believe it should be stats[i] =outcome+ 1;

Related

How toMake Math.random generate number between 1 and 1000? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 months ago.
So I'm trying to figure out how to print out a random number between 1 to 1000.
I tried:
double a = 1+ (Math.random()*1000);
System.out.println(a);
But when I try this i get numbers with a bunch of decimals. I do not want any decimals. Anyone can help? I want to get a value like 50 or 289 or 294. I do not want to get a number like 234.5670242
or 394.220345. Help if you can. will appreciate it. Thank you.
Try this:
public static void main(String args[])
{
// define the range
int max = 1000;
int min = 1;
int range = max - min + 1;
// generate random numbers within 1 to 1000
for (int i = 0; i < 1000; i++) {
int rand = (int)(Math.random() * range) + min;
// Output
System.out.println(rand);
}
}

How to represent a factorial programmatically [duplicate]

This question already has answers here:
print factorial calculation process in java
(3 answers)
Java factorial format
(2 answers)
How do I calculate factorial and show working?
(2 answers)
Closed 4 years ago.
I couldn't find a proper title.
I wrote a tiny program to calculate the factorial of a given integer. The program works fine as expected. Now I would like to also print its representation, say we have 4 as input, the program would output 24 and then print Because 4! = 4 x 3 x 2 x 1.
public class Test {
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
System.out.println("Enter a number:");
Scanner sc = new Scanner(System.in);
int i;
int fact;
int number= sc.nextInt();//Get user input and calculate its factorial
fact = factorial(number);
System.out.println("Factorial of " + number + " is " + fact);
System.out.println("Because " + number+"!" + " = " + number + "x" + (number - 1) ); // This is what I tried so far
}
}
The last println shows what I have tried. However, I'm only able to output 4! = 4 x 3, unable to go down to 1.

Generate 6 digit random number [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
I just want to generate 6 digit random number, and the range should be start from 000000 to 999999.
new Random().nextInt(999999) is returning me number but it is not in 6 digit.
Its as simple as that, you can use your code and just do one thing extra here
String.format("%06d", number);
this will return your number in string format, so the "0" will be "000000".
Here is the code.
public static String getRandomNumberString() {
// It will generate 6 digit random Number.
// from 0 to 999999
Random rnd = new Random();
int number = rnd.nextInt(999999);
// this will convert any number sequence into 6 character.
return String.format("%06d", number);
}
If you need a six digit number it has to start at 100000
int i = new Random().nextInt(900000) + 100000;
Leading zeros do not have effect, 000000 is the same as 0. You can further simplify it with ThreadLocalRandom if you are on Java 7+:
int i = ThreadLocalRandom.current().nextInt(100000, 1000000)
1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].
already solved here
public int gen()
{
Random r = new Random( System.currentTimeMillis() );
return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000));
}
i know it’s very difficult but you can do something like this:
create a class for BinaryNumber;
create a constructor that generate a char[] of 6 character where every single one is generated with a randomiser from 0 to 1
override the toStrig() method so that it will return the digits char[] as a string if you want to display it. then crate a method toInt() that esaminate the string char by char with a for and turn it in a decimal base number by multiplying current digit to 10 to the pow of i:
char[] digits = {‘1’ , ‘0’ , ‘1’ , ‘1’ , ‘0’ , ‘1’};
//random
int result = 0;
for( int i = 0; i < digits.length; i++) {
result += Integer.parseInt(digits[i]) * Math.pow(10, i);
}
return result;
This is the code in java which generate a 6 digit random code.
import java.util.*;
public class HelloWorld{
public static void main(String []args)
{
Random r=new Random();
HashSet<Integer> set= new HashSet<Integer>();
while(set.size()<1)
{
int ran=r.nextInt(99)+100000;
set.add(ran);
}
int len = 6;
String random=String.valueOf(len);
for(int random1:set)
{
System.out.println(random1);
random=Integer.toString(random1);
}
}
}

generate random numbers with no zero [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 5 years ago.
How do I get random number in the range st 1 to 45 only (not included zero). Here is my code so far:
int number;
Random randomNum = new Random();
number = randomNum.nextInt(45)+1;
for (int y = 0; y < 10; y++) {
System.out.println("");
for (int i = 1; i <=6; i++) {
number= randomNum.nextInt(45);
if (i==6) {
System.out.printf("%d",number);
}
else {
System.out.printf("%d-",number);
}
}
}
randomNum.nextInt(45) + 1 will generate a number from 0 to 44 and add 1, thus generating numbers from 1 to 45.

value zero generated numerously using random function() [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
/*This is my function code*/
Random random = new Random();
int randomInt = random.nextInt()%200;
String imgName = "img" + randomInt;
int ImageId = getResources().getIndentifier(imgName,"drawabale",getPackageName());
myImage.setImageResourse(ImageId);
Previously in my drawable folder there are
200 images already inserted using img1,img2.....img199
like nomenclature...
every time I call random function mention below to generated one
random number and form a string name starting from
"img" and some number. But most of time only 0 is generated by random function and id set to
image is display 0th image constantly..at some point it successfully display other images but most of time it generated zero value continuosly.
Thanks in Advance !
You can generate Random number with specific Range
Random r = new Random();
int randomInt = r.nextInt(maxVal - minVal) + minVal
For your example
int randomInt = r.nextInt(200 - 1) + 1
Will generate number between 1 to 199.
A random number generated by nextInt should be a multiple of 200 at random, i.e. one every 200 or so. This test suggests this is happening correctly:
public void test() {
Random random = new Random();
final int tests = 200000;
int twoHunderdIsAFactor = 0;
for (int i = 0; i < tests; i++) {
if (random.nextInt() % 200 == 0) {
twoHunderdIsAFactor += 1;
}
}
System.out.println("Tests = " + tests + " Hits = " + twoHunderdIsAFactor + " Proportion = " + (tests / twoHunderdIsAFactor));
}
prints Tests = 200000 Hits = 1012 Proportion = 197 - i.e. about 200 times for 200000 randoms.
Generating randoms in a specific range is dealt with in other answers.

Categories

Resources