I have managed to print 200 random numbers, from those numbers I want the program to return the largest number out of it.
import java.util.Random;
class random {
public static int genRandom() {
return new Random().nextInt(1000000);
}
public static void main(String[] args) {
Random ran = new Random();
for (int i = 0; i < 200; i++) {
System.out.println(ran.nextInt(1000000));
}
}
}
Not the best answer but according to your code you can use :
static int maxNumber = 0;
public static int genRandom() {
return new Random().nextInt(1000000);
}
public static void main(String[] args) {
for (int i = 0; i < 200; i++) {
int randomNumber = genRandom();
if (randomNumber > maxNumber) {
maxNumber = randomNumber;
}
System.out.println(randomNumber);
}
System.out.println("The largest number is: " + maxNumber);
}
Java Code
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
Random ran = new Random();
ArrayList<Integer> randNum = new ArrayList<Integer>();
for (int i = 0; i < 200; i++) {
//System.out.println(ran.nextInt(1000000));
randNum.add(ran.nextInt(1000000));
}
//System.out.print(randNum);
Collections.sort(randNum);
System.out.println(randNum);
System.out.println("Largest Number is " + randNum.get(randNum.size()-1));
}
}
Related
I have tested the random number generator and it works fine outside of the app. It's a fairly standard piece of code. I created getters and setters, and NetBeans does not complain about anything being passed to the application, but what should be a random number is a "0". I would like to understand more about getters and setters and classes. I may have designated something incorrectly.
//This is the class code
public class RandomGen {
int minSeed = 100;
int maxSeed = 999;
int num;
public static void RandomNum(int minSeed, int maxSeed) {
Random rand = new Random();
int num = rand.nextInt(maxSeed - minSeed) + 1;
}
public void setMin(int minSeed) {
this.minSeed = minSeed;
}
public void setMax(int maxSeed) {
this.maxSeed = maxSeed;
}
public int getNum() {
return num;
}
}
This is the code within main in my application
RandomGen rnumber = new RandomGen();
int rnum = rnumber.getNum();
int bigSeed = rnumber.maxSeed;
int smallSeed = rnumber.minSeed;
System.out.println("Random Number = " + rnum);
System.out.println("MaxSeed = " + bigSeed);
System.out.println("MinSeed = " + smallSeed);
This is the printed return
Random Number = 0
MaxSeed = 999
MinSeed = 100
You have created a local variable so use the global as
num = rand.nextInt(maxSeed - minSeed) + 1;
As per your code , you need to call that static method to initialize num as well so
RandomGen rnumber = new RandomGen();
RandomGen.RandomNum(rnumber.minSeed, rnumber.maxSeed);
//^^^^^^^^^^^^^^^^^^^^^
int rnum = rnumber.getNum();
int bigSeed = rnumber.maxSeed;
int smallSeed = rnumber.minSeed;
Note: constructors cannot be static, can't have any return type
static int num; // global to class, different from the `num` inside RandomNum
public static void RandomNum(int minSeed, int maxSeed)
{
Random rand = new Random();
// local variable
// changes here, can only be seen inside RandomNum method
//int num = rand.nextInt(maxSeed - minSeed) + 1;
num = rand.nextInt(maxSeed - minSeed) + 1;
}
first
static int num; // must also be static
second
public static void RandomNum(int minSeed, int maxSeed)
{
Random rand = new Random();
num = rand.nextInt(maxSeed - minSeed) + 1; // remove the declaration you try to make it local variable
}
This is incorrect.
Try this:
import java.util.Random;
public class RandomGen {
private Random random;
private int minRange;
private int maxRange;
public static void main(String[] args) {
int minRange = (args.length > 0) ? Integer.parseInt(args[0]) : 100;
int maxRange = (args.length > 1) ? Integer.parseInt(args[1]) : 200;
int numValues = (args.length > 2) ? Integer.parseInt(args[2]) : 10;
RandomGen randomGen = new RandomGen(minRange, maxRange);
for (int i = 0; i < numValues; ++i) {
System.out.println(randomGen.getNextIntInRange());
}
}
public RandomGen(int minRange, int maxRange) {
this(minRange, maxRange, null);
}
public RandomGen(int minRange, int maxRange, Long seed) {
this.minRange = minRange;
this.maxRange = maxRange;
this.random = (seed == null) ? new Random() : new Random(seed);
}
public int getNextIntInRange() {
return this.minRange + this.random.nextInt(this.maxRange-this.minRange)+1;
}
}
i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?
this is my code
import java.util.*;
public class prova1{
public int min;
public void tabele(){
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
System.out.println(e[i]);
}
}
public void emin(){
Random r = new Random();
int d;
int e[]=new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
if(e[i]<min){
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
prova.tabele();
prova.emin();
}
}
Return the array from tabele to a local variable and send it as parameter to emin
import java.util.*;
public class prova1 {
public int min;
public int[] tabele() {
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(99) + 1;
e[i]=d;
System.out.println(e[i]);
}
return e;
}
public void emin(int[] e) {
Random r = new Random();
int d;
for(int i=0; i<e.length;i++) {
if(e[i]<min) {
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
int[] arr = prova.tabele();
prova.emin(arr);
}
}
I wrote this code that random picks two cards from a deck, but I'm having trouble trying to get it to compare, I've run into some issues and want to work that out step by step but unsure how to compare both statements with only one defined variable?
package question1;
public class HouseOfCards {
public static void main(String[] args) {
String [] SuitNames= {
"Spades","Diamonds","Clubs","Hearts"
};
String [] CardNames= {
"Ace","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"
};
String SuitName="hi",SuitName2="die";
String CardName="hi", CardName2="die";
for (int i=0; i<=1; i++){
int numCard=52;
int randNum=(int)(Math.random() * numCard);
int randNum2=(int)(Math.random() * numCard);
int suitNum = randNum / 13;
int cardNum = randNum % 13;
int suitNum2 = randNum2 / 13;
int cardNum2 = randNum2 % 13;
SuitName = SuitNames[suitNum];
CardName = CardNames[cardNum];
SuitName2 = SuitNames [suitNum2];
CardName2 = CardNames[cardNum2];
System.out.println(CardName + " of " + SuitName);
}
if (CardName.equals(CardName2)){
System.out.println("Same Rank");
}
else if (SuitName.equals(SuitName2)){
System.out.println("Same Suit");
}
}
}
i edited my code so that it displays if its the same rank between both random picked cards or same suit between random picked cards, but sometime it doesn't print the statement, why?
Try something like this:
public class HouseOfCards {
public class HouseOfCards {
public static void main(String[] args) {
int Spades=1;
String [] suitNames= {
"Spades","Diamonds","Clubs","Hearts"
};
String [] cardNames= {
"Ace","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"
};
String card1="";
String card2="";
String suitName="";
String cardName1="";
String suitName2="";
String cardName2="";
int cardPosition1 = 0;
int cardPosition2 = 0;
for (int i=0; i<=1; i++){
int numCard=52;
int randNum=(int)(Math.random() * numCard);
int suitNum = randNum / 13;
int cardNum = randNum % 13;
if(i==0) {
suitName = suitNames[suitNum];
cardName1 = cardNames[cardNum];
card1 = cardName1+" of "+suitName;
}
else {
suitName2 = suitNames[suitNum];
cardName2 = cardNames[cardNum];
card2 = cardName2+" of "+suitName2;
}
}
System.out.println(card1);
System.out.println(card2);
for (int i = 0; i < cardNames.length; i++) {
if(cardName1.equals(cardNames[i])) {
cardPosition1 = i;
}
if(cardName2.equals(cardNames[i])) {
cardPosition2 = i;
}
}
System.out.println(cardPosition1);
System.out.println(cardPosition2);
if(cardPosition1>cardPosition2) {
System.out.println("First card has bigger number");
}
else if (cardPosition1<cardPosition2) {
System.out.println("Second card has bigger number");
}
else if (cardPosition1==cardPosition2) {
System.out.println("Equal cards (probably different suites you can check it further)");
}
}
}
anyone has any idea how can I limit this program to print only 5 of the 20 elements of the array.
Greetings and thanks.
import java.util.*;
public class lot {
public static void main(String[] args) {
int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Random rd = new Random();
for (int i = 0; i < n.length; i++) {
System.out.println(rd.nextInt(n[i]) + 1);
}
}
}
import java.util.*;
public class Lot {
public static void main(String[] args) {
int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Random rd = new Random();
for (int i = 0; i < 5; i++) {
System.out.println(rd.nextInt(n[i]) + 1);
}
}
}
Learn how to use a for loop http://en.wikipedia.org/wiki/For_loop#Java
hi i am doing a coin toss simulator for java that must be done a certain way. it must have a string for sideup to hold the string of "heads" or "tails" made by a no arg constructor, the toss method must be void and it must have a getsideup method, then we must run the coin toss 20 times and diplay the number of heads and tails... i can do it easy with none void methods and just returning the result, but getting around this void and getsideup is driving me nuts.
this is what i have so far.
import java.util.Random;
public class coin {
public static String sideUp;
public static void toss() {
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0) {
sideUp = "heads";
} else {
sideUp = "tails";
}
}
public static String getsideup() {
System.out.println(sideUp);
return sideUp;
}
public static void main(String[] args) {
// coin coin = new coin();
int hcount = 0;
int tcount = 0;
for (int i = 1; i <= 20; i++) {
if (getsideup().equals("heads")) {
hcount++;
} else {
tcount++;
}
}
System.out.println("total heads = " + hcount + " total tails = " + tcount);
}
}
im hoping someone can tell me what im doing wrong and put me in the right direction.
You're not calling toss() at the beginning of your loop. That's required to set a value to sideUp, and required to give sideUp to change every toss.
The simple fix is put toss in your for loop before the if statement but I see a lot that can be done here. First I would add a constructor for the Coin class and add hcount and tcount to the class variables and make heads and tails constants:
private String sideUp;
private int hcount;
private int tcount;
private static final String HEADS = "Heads";
private static final String Tails = "Tails";
Coin()
{
this.sideUp = HEADS;
this.hcount = 0;
this.tcount = 0;
}
Then I would make a method to check the toss:
public void checkToss()
{
if (getsideup().equals(HEADS))
hcount++;
else
tcount++;
}
Now add the toss() and checkCoin() method to the for loop before the if statement. It should look like this:
for (int i = 1; i <= 20; i++)
{
coin.toss();
coin.checkToss();
}
I would also make a getter for the heads count and tails count:
public int getHeadsCount()
{
return this.hcount;
}
public int getTailsCount()
{
return this.tcount;
}
Everything put together looks like this:
import java.util.Random;
public class Coin
{
private String sideUp;
private int hcount;
private int tcount;
private static final String HEADS = "Heads";
private static final String TAILS = "Tails";
Coin()
{
this.sideUp = HEADS;
this.hcount = 0;
this.tcount = 0;
}
public void toss()
{
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0)
sideUp = HEADS;
else
sideUp = TAILS;
}
public String getsideup()
{
System.out.println(sideUp);
return sideUp;
}
public void checkToss()
{
if (getsideup().equals(HEADS))
this.hcount++;
else
this.tcount++;
}
public int getHeadsCount()
{
return this.hcount;
}
public int getTailsCount()
{
return this.tcount;
}
public static void main(String[] args)
{
Coin coin = new Coin();
for (int i = 1; i <= 20; i++)
{
coin.toss();
coin.checkToss();
}
System.out.println("Total Heads = " + coin.getHeadsCount() + " Total Tails = " + coin.getTailsCount());
}
}
import java.util.Random;
public class coin
{
public static String sideUp;
public int hcount=0;
public int tcount=0;
public static void toss()
{
Random rand = new Random();
int sideup = rand.nextInt(2);
if (sideup == 0)
{
sideUp = "heads";
hcount++;
}
else
{
sideUp = "tails";
tcount++;
}
}
public static void main(String[] args)
{
for(int i=0;i<20;i++)
{toss();}
System.out.println("total heads = " + hcount + " total tails = " + tcount);
}
}
for(int i = 1; i <= 20; i++)
{
toss();
if (getsideup().equals("heads"))
{
hcount++;
} else
{
tcount++;
}
}