This question already has answers here:
Java random numbers using a seed
(7 answers)
Closed 4 years ago.
I'm a novice Java programmer and need to create two random numbers. We were instructed to use System.currentTimeMillis() however I don't know why I am getting so many repeated numbers.
import java.util.Random;
public class TestClass1 {
public static void main(String[] args) {
int points = 0;
while (points < 100) {
int[] scoreInfo = diceGen();
System.out.println(scoreInfo[0]);
System.out.println(scoreInfo[1]);
points += 1;
}
}
public static int[] diceGen() {
Random num = new Random(System.currentTimeMillis());
int dice1 = num.nextInt(6)+1;
int dice2 = num.nextInt(6)+1;
int[] numbers = {dice1, dice2};
return numbers;
}
}
Output:
6
6
6
6
6
6
6
6
6
6
6
6
6
6
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
4
2
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
3
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
1
4
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
2
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
6
5
The parameter to the Random() constructor is the seed to the random number generator. Every time you create a new Random instance with the same seed, it will generate the same numbers. Since an execution of diceGen() takes less than a millisecond, you're creating multiple instances with the same millisecond count.
Instead, you need to create a single Random instance and store it in a field or pass it as a parameter.
The code is executing fast enough that between two iterations of the loop, the value returned by System.currentTimeMillis() remains the same. Those Random instances are therefore created with the same seed and return the same values.
Consider using System.nanoTime() or construct a single Random instance and reuse it in all of your iterations.
Something like
public static void main(String[] args) {
int points = 0;
Random num = new Random(System.nanoTime());
while (points < 100) {
int[] scoreInfo = diceGen(num);
System.out.println(scoreInfo[0] + ", " + scoreInfo[1]);
points += 1;
}
}
public static int[] diceGen(Random num) {
int dice1 = num.nextInt(6) + 1;
int dice2 = num.nextInt(6) + 1;
int[] numbers = { dice1, dice2 };
return numbers;
}
Make the rng global in scope. Each call will change the number. If you seed the rng each time you call the generator, chances are you will call the seed on the same tick and so get the same number.
import java.util.Random;
public class TestClass1 {
static public Random num = new Random(System.currentTimeMillis());
public static void main(String[] args) {
int points = 0;
while (points < 100) {
int[] scoreInfo = diceGen();
System.out.println(scoreInfo[0]);
System.out.println(scoreInfo[1]);
points += 1;
}
}
public static int[] diceGen() {
int dice1 = num.nextInt(6)+1;
int dice2 = num.nextInt(6)+1;
int[] numbers = {dice1, dice2};
return numbers;
}
}
The way rngs work is x = trunc( old_x * big_prime ) + prime and on first call old_x is the seed.
I am trying to print out a Right Triangle that looks like this:
1
2 1
3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
The size of the triangle increases if the number in the method gets larger, which in this case is 11.
My code seems to only work up to 10 as after 10, my spacing is messed up.
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
I am trying to make it so that up to 99, the spacing is correct. What kind of edits should I do to my if statements or for loops in order to space it properly?
Code:
public class Patterns
{
public static void main(String[] args)
{
displayPattern(13);
//displayPattern(11,",");
}
public static void displayPattern(int n)
{
//print out n-1 spaces and the first number
//print n-2 spaces and the 2nd then first number
int counter = n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= counter; j++)
{
if (n > 10)
{
if (i == n)
{
System.out.print("");
}
else if (i <= 10)
{
System.out.print(" ");
}
else
{
System.out.print(" ");
}
}
else if (n <=10)
{
if (i>9)
{
System.out.print(" ");
}
else
{
System.out.print(" ");
}
}
}
System.out.print(i + " ");
int tempValue = i - 1;
while(tempValue>0)
{
System.out.print(tempValue);
if(tempValue>1)
{
System.out.print(" ");
}
tempValue--;
}
if(tempValue==0)
{
System.out.print("\n");
}
counter--;
}
}
}
This is currently what I have for my Palindrome program for my computer science class. I have it pretty much working, except whenever a word is a palindrome, it is an infinite loop. I know I have to insert a number base case, but I do not how to do that...I'm really having trouble understanding recursion. Help is appreciated.
public class PalindromeTester
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
String str, another = "y";
int left, right;
while (another.equalsIgnoreCase("y"))
{
System.out.println("Enter a potential palindrome:");
str = scan.next();
left = 0;
right = str.length() - 1;
tester(str, left, right);
System.out.println();
System.out.println("Test another palindrome (y/n)?");
another = scan.next();
}
}
public static void tester (String str, int left, int right)
{
Scanner scan = new Scanner (System.in);
while (str.charAt(left) == str.charAt(right) && left < right)
{
System.out.println(str);
tester( str, left + 1, right -1);
}
if (left < right)
{
System.out.println("That string is NOT a palindrome.");
}
else
{
System.out.println("That string IS a palindrome.");
}
}
}
You are using a while loop. With recursion, this is done implicitly.
You have to split the algorithm in small parts.
[] represents left, {} represents right.
[1] 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} -->Level 0
1 [2] 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 -->Level 1
1 2 [3] 4 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 -->Level 2
1 2 3 [4] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 -->Level 3
1 2 3 4 [5] 6 7 8 9 0 9 8 7 6 {5} 4 3 2 1 -->Level 4
1 2 3 4 5 [6] 7 8 9 0 9 8 7 {6} 5 4 3 2 1 -->Level 5
1 2 3 4 5 6 [7] 8 9 0 9 8 {7} 6 5 4 3 2 1 -->Level 6
1 2 3 4 5 6 7 [8] 9 0 9 {8} 7 6 5 4 3 2 1 -->Level 7
1 2 3 4 5 6 7 8 [9] 0 {9} 8 7 6 5 4 3 2 1 -->Level 8
1 2 3 4 5 6 7 8 9 {[0]} 9 8 7 6 5 4 3 2 1 -->Level 9
So, tester will continue until:
We've reached the middle of the word.
The word is not a palindrome
Example of case 2:
[1] 2 3 A 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1}
1 [2] 3 A 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1
1 2 [3] A 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1
1 2 3 [A] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 --> !!!
I thought this method would be very helpful for the understanding of how is this recursion working
public static String positions(String word, int l, int r) {
char[] a = word.toCharArray();
String s = "";
// [letter] if left, {} if right, [{}] if both
for (int i = 0; i < a.length; i++) {
if (l == i && r == i) {
s += "{[" + a[i] + "]}";
} else if (l == i) {
s += "[" + a[i] + "]";
} else if (r == i) {
s += "{" + a[i] + "}";
} else {
s += a[i];
}
s+=" ";
}
return s;
}
And finally, the tester method.
public static boolean tester(String str, int left, int right) {
System.out.println(positions(str, left, right) +" tester(str, "+left +", "+right+")");
if (left>=right) // case 1
return true; // that's ok, we've reached the middle
// the middle was not reached yet.
// is the condition satisfied?
if (str.charAt(left) == str.charAt(right)) {
// yes. So, lets do it again, with the parameters changed
return tester(str, left + 1, right - 1);
}
//the condition was not satisfied. Let's get out of here.
else {
return false;
}
}
Some outputs:
Enter a potential palindrome:
1234567890987654321
[1] 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 {1} tester(str, 0, 18)
1 [2] 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 {2} 1 tester(str, 1, 17)
1 2 [3] 4 5 6 7 8 9 0 9 8 7 6 5 4 {3} 2 1 tester(str, 2, 16)
1 2 3 [4] 5 6 7 8 9 0 9 8 7 6 5 {4} 3 2 1 tester(str, 3, 15)
1 2 3 4 [5] 6 7 8 9 0 9 8 7 6 {5} 4 3 2 1 tester(str, 4, 14)
1 2 3 4 5 [6] 7 8 9 0 9 8 7 {6} 5 4 3 2 1 tester(str, 5, 13)
1 2 3 4 5 6 [7] 8 9 0 9 8 {7} 6 5 4 3 2 1 tester(str, 6, 12)
1 2 3 4 5 6 7 [8] 9 0 9 {8} 7 6 5 4 3 2 1 tester(str, 7, 11)
1 2 3 4 5 6 7 8 [9] 0 {9} 8 7 6 5 4 3 2 1 tester(str, 8, 10)
1 2 3 4 5 6 7 8 9 {[0]} 9 8 7 6 5 4 3 2 1 tester(str, 9, 9)
true
Test another palindrome (y/n)?
y
Enter a potential palindrome:
12345A678654321
[1] 2 3 4 5 A 6 7 8 6 5 4 3 2 {1} tester(str, 0, 14)
1 [2] 3 4 5 A 6 7 8 6 5 4 3 {2} 1 tester(str, 1, 13)
1 2 [3] 4 5 A 6 7 8 6 5 4 {3} 2 1 tester(str, 2, 12)
1 2 3 [4] 5 A 6 7 8 6 5 {4} 3 2 1 tester(str, 3, 11)
1 2 3 4 [5] A 6 7 8 6 {5} 4 3 2 1 tester(str, 4, 10)
1 2 3 4 5 [A] 6 7 8 {6} 5 4 3 2 1 tester(str, 5, 9)
false
Test another palindrome (y/n)?
In the main method,
System.out.println(tester(str, left, right));
In order to see the true/false output
Since your are using recursion (in its basic purposes mostly used to eliminate loops), isn't your while loop inside the tester() method supposed to be an if?
public static void tester (String str, int left, int right)
{
Scanner scan = new Scanner (System.in);
if (str.charAt(left) == str.charAt(right) && left < right)
{
System.out.println(str);
tester( str, left + 1, right -1);
}
else if (left < right)
{
System.out.println("That string is NOT a palindrome.");
}
else
{
System.out.println("That string IS a palindrome.");
}
}
I modified your tester() method and replaced your while with an if and moved your second if clause.
public static void tester(String str, int left, int right) {
if (str.charAt(left) == str.charAt(right) && left < right) {
tester(str, left + 1, right - 1);
} else {
if (left < right) {
System.out.println("That string is NOT a palindrome.");
} else {
System.out.println("That string IS a palindrome.");
}
}
}
So I have a simple program that creates a LinkedList array of given size n, with each value in the list representing a new separate LinkedList data Structure.
public class Graph {
public final LinkedList[] graph;
public Graph(int n){
graph = new LinkedList[n];
for (int i=0; i<n; i++){
graph[i] = new LinkedList();
}
}
public void addEdge(int x, int y){
graph[x].addFirst(y);
graph[y].addFirst(x);
}
For some reason, however, when I call the addEdge() method with two int values, instead of adding them to the specific called LinkedList in graph[], it adds them to every LinkedList in graph[].
What is the problem here?
Edit:*
public void addEdge(int x, int y){
graph[x].addFirst(y);
graph[y].addFirst(x);
for (int i=0; i<graph.length; i++){
Node tmp = graph[i].first;
System.out.println(i + ":");
while (tmp != null){
System.out.print(tmp.name + " ");
tmp = tmp.Rnext;
}
System.out.println();
}
System.out.println();
}
public class Test {
public static void main(String[] args) {
Graph myGraph1 = new Graph(8);
myGraph1.addEdge(1, 2);
myGraph1.addEdge(1, 7);
myGraph1.addEdge(1, 4);
myGraph1.addEdge(2, 5);
myGraph1.addEdge(2, 6);
myGraph1.addEdge(6, 3);
myGraph1.addEdge(3, 8);
myGraph1.addEdge(5, 7);
}
}
Here is the output of graph:
0:
1 2
1:
1 2
2:
1 2
3:
1 2
4:
1 2
5:
1 2
6:
1 2
7:
1 2
0:
1 7 1 2
1:
1 7 1 2
2:
1 7 1 2
3:
1 7 1 2
4:
1 7 1 2
5:
1 7 1 2
6:
1 7 1 2
7:
1 7 1 2
0:
1 4 1 7 1 2
1:
1 4 1 7 1 2
2:
1 4 1 7 1 2
3:
1 4 1 7 1 2
4:
1 4 1 7 1 2
5:
1 4 1 7 1 2
6:
1 4 1 7 1 2
7:
1 4 1 7 1 2
0:
2 5 1 4 1 7 1 2
1:
2 5 1 4 1 7 1 2
2:
2 5 1 4 1 7 1 2
3:
2 5 1 4 1 7 1 2
4:
2 5 1 4 1 7 1 2
5:
2 5 1 4 1 7 1 2
6:
2 5 1 4 1 7 1 2
7:
2 5 1 4 1 7 1 2
0:
2 6 2 5 1 4 1 7 1 2
1:
2 6 2 5 1 4 1 7 1 2
2:
2 6 2 5 1 4 1 7 1 2
3:
2 6 2 5 1 4 1 7 1 2
4:
2 6 2 5 1 4 1 7 1 2
5:
2 6 2 5 1 4 1 7 1 2
6:
2 6 2 5 1 4 1 7 1 2
7:
2 6 2 5 1 4 1 7 1 2
0:
6 3 2 6 2 5 1 4 1 7 1 2
1:
6 3 2 6 2 5 1 4 1 7 1 2
2:
6 3 2 6 2 5 1 4 1 7 1 2
3:
6 3 2 6 2 5 1 4 1 7 1 2
4:
6 3 2 6 2 5 1 4 1 7 1 2
5:
6 3 2 6 2 5 1 4 1 7 1 2
6:
6 3 2 6 2 5 1 4 1 7 1 2
7:
6 3 2 6 2 5 1 4 1 7 1 2
Here is the LinkedList and Node Class I am using:
import java.util.NoSuchElementException;
public class LinkedList {
public static Node first;
public LinkedList(){
first = null;
}
// Returns true if the list is empty
public boolean isEmpty(){
return first == null;
}
// Inserts a new node at the beginning of this list.
public void addFirst(int name){
first = new Node(name, first);
}
public boolean findData(int d){
if(first == null) throw new NoSuchElementException();
Node tmp = first;
while (tmp != null) {
if (tmp.name == d) return true;
tmp = tmp.Rnext;
} return false;
}
}
public class Node {
public int name;
public Node Rnext;
public Node(){
name = 0;
Rnext = null;
}
public Node(int n, Node r){
this.name = n;
this.Rnext = r;
}
}
public static Node first;
This is the problem. Every single LinkedList you make is sharing the same Node, so they're all effectively the same list.
Don't use static for instance variables.
import java.util.Scanner;
public class Assignment5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a;
System.out.println("Please enter a number between 1 and 9:");
int[] graph = new int[10];
for(int i = 0; i < 10; ++i){
a = scan.nextInt();
graph[i] = a;
}
}
// this is the method I'm required to use for
// this array assignment
public static void printVertical(int[] graph){
for(int i = 0; i < graph.length; i++){
for(int j = 0; j < graph[i]; j++){
System.out.print(" " + graph[i]);
}
System.out.println();
}
}
// this is what I've got for the horizontal method
// I took the for loop and nested for loop from the vertical.
// I'm stuck on changing the variable to flip the graph -90 degrees
public static void printHorizontal(int[] graph){
for(int i = 0; i < graph.length; i++){
for(int h = 0; h < graph[i]; h++){
System.out.print(" " + graph[i]);
}
System.out.println();
}
}
public static int calculateTotal(int[] graph){
int sum = 0;
for( int num : graph ){
sum = sum + num;
}
System.out.print("The total is: " + sum);
return sum;
}
}
My for loop in the printVertical method isn't returning the graph properly.
What am I doing wrong?
The output needs to look like this:
Sample output:
2 4 6 8 9 8 6 4 3 2
---- Vertical Graph ----
2 2
4 4 4 4
6 6 6 6 6 6
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
6 6 6 6 6 6
4 4 4 4
3 3 3
2 2
---- Horizontal Graph ----
9
8 9 8
8 9 8
6 8 9 8 6
6 8 9 8 6
4 6 8 9 8 6 4
4 6 8 9 8 6 4
2 4 6 8 9 8 6 4 2
The total is: 52
2 4 6 8 9 8 6 4 2
Consider the code given below.It might solve your problem..
import java.util.Scanner;
public class Assignment5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a;
System.out.println("Please enter a number between 1 and 9:");
int[] graph = new int[10];
for(int i = 0; i < 10; ++i){
a = scan.nextInt();
graph[i] = a;
}
System.out.println("----------Vertical Graph-------------");
printVerticalGraph(graph);
System.out.println("----------Horizontal Graph-------------");
printHorizontalGraph(graph);
}
public static void printVerticalGraph(int[] graph)
{
for (int i = 0 ; i < graph.length; i++)
{
for (int j = 0 ; j < graph[i] ; j++)
{
System.out.print(graph[i]+" ");
}
System.out.println();
}
}
public static void printHorizontalGraph(int[] graph)
{
for (int i = 0 ; i < graph.length; i++)
{
for (int j = 0 ; j < graph.length; j++ )
{
if (i > 10 - graph[j])
{
System.out.print(graph[j]);
}
else
System.out.print( " ");
System.out.print(" ");
}
System.out.print("\n");
}
}
}
Here is the sample output..
Please enter a number between 1 and 9:
2
4
6
8
9
8
6
4
3
2
----------Vertical Graph-------------
2 2
4 4 4 4
6 6 6 6 6 6
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
6 6 6 6 6 6
4 4 4 4
3 3 3
2 2
----------Horizontal Graph-------------
9
8 9 8
8 9 8
6 8 9 8 6
6 8 9 8 6
4 6 8 9 8 6 4
4 6 8 9 8 6 4 3
2 4 6 8 9 8 6 4 3 2
package Kashish;
public class arrayseries1 {
public static void main(String[] args) {
int arr[]={2,5,4,1,6,9};
System.out.println("--Horizontal--");
for (int i=0;i<arr.length;i++)
{
int j=arr[i];
for(int k=0;k<j;k++)
{
System.out.print(j);
}
System.out.print("\n");
}
System.out.println("--Vertical--");
int max=0; /// take the max element
for(int i=0;i<arr.length;i++) ///to find max
{
if(arr[i]>max)
{
max=arr[i];
}
}
int x=max;
for(int i=0;i<x;i++)
{
//int j=arr[i];
for(int j=0;j<arr.length;j++)
{
int k=max-arr[j];
if(k>0)
System.out.print(" ");
else
System.out.print(arr[j]);
System.out.print(" ");
}System.out.print("\n");
max--;
}
}
}