Remember Old Random Numbers? Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In order to prevent duplicates in my random number generator, I want to make an if statement that tests if my random number (x) is the same as any other previous values of x. If it is, then it returns and restarts the loop.
How do I have my code remember all of the previous values of x?
Pseudocode:
if (x == previous value of x) {
return;
}

Explanation: For example, you can make a list that contains 8 numbers and after that shuffle the list in order to be randomized what you want.
Code:
Integer[] array = new Integer[100];
for (int i = 0; i < 100; i++) {
array[i] = i+1;
}
List<Integer> list = Arrays.asList(array);
Collections.shuffle(list);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
Output:
88 25 24 15 92 87 14 100 68 98 91 32 28 22 74 37 10 90 44 77 94 52 34
86 72 99 46 71 36 26 69 55 82 79 19 78 60 49 70 85 33 66 45 57 61 9 51
76 3 42 16 29 84 93 58 2 53 97 6 17 13 43 12 56 35 96 83 40 38 59 50 5
41 65 11 23 73 4 47 89 1 81 21 80 63 48 20 27 64 54 18 75 30 62 39 31
7 8 67 9

Use a list to record the previous values of x. List are better than arrays for this purpose, because you don't need to resize them if you want to generate more random numbers.
List<int> randomNumberList = new ArrayList<int>();
randomNumberList.add(randomNumberThatYouGenerated);
then just use an if:
if (randomNumberList.contains(randomNumberThatYouGenerated)) {
//code
}

Use a array to record the previous value of x..... and check if the currently value inside that array..

Related

Output 12 Times Tables Java

I am trying to output 12 times tables. Where it will go through all the times tables from 1 to 12. And each times table will go up to *12. For examples, the output should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
...
12 24 36 48 60 72 84 96 108 120 132 144
Here is the code I have so far, I seem to be very close. But I am struggling with perfecting it.
public class Tables {
public void generateTable()
{
//Put the code for your times-table here
int i;
int j;
for(i=1; i<=12; i++)
{
for(j=i; j<=i*12; j = j+1)
{
System.out.print(j*i + " ");
}
System.out.println();
}
}
public static void main(String args[])
{
Tables t = new Tables();
t.generateTable();
}
}
I am receiving the following output for this code:
1 2 3 4 5 6 7 8 9 10 11 12
4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108
...
In the inner loop, you only want to go from 1 to 12 instead of from i to i*12.
You could also declare i and j more local in the respective loop where the initialization is done.

Regarding printf use in nested for loop (multiplication table)

Have a problem with my output when creating a multiplication table 1-12 inclusive. below is my code and output
public class Multiplication
{
public static void main(String[]args)
{
int row,column;
System.out.println("Below is a multiplication table from 1-12 inclusive");
for (row=1;row<=12;row++)
{
System.out.printf("%4d\n",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
}
}
}
and my output is
Below is a multiplication table from 1-12 inclusive
1
1 2 3 4 5 6 7 8 9 10 11 12 2
2 4 6 8 10 12 14 16 18 20 22 24 3
3 6 9 12 15 18 21 24 27 30 33 36 4
4 8 12 16 20 24 28 32 36 40 44 48 5
5 10 15 20 25 30 35 40 45 50 55 60 6
6 12 18 24 30 36 42 48 54 60 66 72 7
7 14 21 28 35 42 49 56 63 70 77 84 8
8 16 24 32 40 48 56 64 72 80 88 96 9
9 18 27 36 45 54 63 72 81 90 99 108 10
10 20 30 40 50 60 70 80 90 100 110 120 11
11 22 33 44 55 66 77 88 99 110 121 132 12
12 24 36 48 60 72 84 96 108 120 132 144
My problem is in getting the far right column to be on the far left. I've tried researching why only the number '1' appears and then it jumps but cant find anything
Your problem is the first of your two printf statements. You should just be able to replace it with System.out.printf("\n");.
(The "jump" to which you refer is a newline, which is represented by the \n character.)
Try this:
public class Multiplication
{
public static void main(String[]args)
{
int row,column;
System.out.println("Below is a multiplication table from 1-12 inclusive");
for (row=1;row<=12;row++)
{
System.out.printf("%4d",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
System.out.printf("\n");
}
}
}
for (row=1;row<=12;row++)
{
System.out.printf("%4d",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
if(column==12) System.out.printf("\n");
}

Load a file into java and make an array from the data

I'm running into a problem whenever I try to take the data from a file and make an array and an int out of that data.
For this specific problem, we're given 17 data points in this style:
13 24 35 46 57 68 79 80 32 43 54 65 76 87 98 27
438
What I'm trying to do is make an array from the first row, and make the second row's single variable into an int.
How would I do this in java?
(for the problem, the second variable is what we check our results against, so I do want it as an int or atleast in it's own array)
You can use this :
String line = "13 24 35 46 57 68 79 80 32 43 54 65 76 87 98 27";
String[] array = line.split(" ");
If you want to use this numbers for counting, you need to change them to integer.
int[] intarray = new int[array.length];
for (int i = 0; i < array.length; i++) {
intarray[i] = Integer.valueOf(array[i]);
}

Problem with Project Euler problem 18 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I'm currently trying to solve problem 18 of project Euler. The goal is:
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
I tried to solve it with the following algorithm:
public static void main(String[] args) throws NumberFormatException, IOException {
int[][] values = readFile();
int depth = values.length - 2;
while ( depth >= 0) {
for (int j = 0; j < depth; j++) {
values[depth][j] += Math.max( values[depth+1][j], values[depth+1][j+1]);
}
depth += -1;
}
values[0][0] += Math.max(values[1][0], values[1][1]);
System.out.println("The maximum path sum is: " + values[0][0]);
}
The array values contains all the numbers from the triangle where values[0][0] is the element in the top row and values[n][n] is the last element in the last row.
The algorithm uses the approach that if for example I start in the last row and have the choice between 04 + 63 and 62 + 63, the sum of the field in which 63 was will be set to 125 because this is the highest amount. This algorithm works for the small triangle, but for the big triangle it seems to fail. I'm not sure why and would appreciate every hint.
I believe that the line:
for (int j = 0; j < depth; j++) {
should be
for (int j = 0; j <= depth; j++) {
because right now you aren't visiting the last element on each row. Of course, then you don't need the line
values[0][0] += Math.max(values[1][0], values[1][1]);
because it is already done in the loop.
I don't know the correct algorithm, but there's an easy proof of #Johns comment on the question, that the best local choice doesn't necessarily lead to the best global solution.
Consider this (extreme) example:
1
1 0
1 0 1000
1 0 0 0
1 0 0 0 0
Given your algorithm, you'd obviously go down the very left of the path and never read the 1000 that must be on the best path.
This may not be the best solution, but what if for each iteration you kept track of the sum to that point. Then when you go to the last row the max value would be your answer.

Java: Simple Combination of a set of element of k order

I have a set of numbers {'1','13','25','32','49',...}, i want to calculate all possible combinations of this numbers of order k.
Esample1:
set = {'1','5','23','41,'54','63'};
k = 4;
Output1:
1 5 23 41
1 5 23 54
1 5 23 63
1 5 41 54
1 5 41 63
1 5 54 63
1 23 41 54
1 23 41 63
1 23 54 63
1 41 54 63
5 23 41 54
5 23 41 63
5 23 54 63
5 41 54 63
23 41 54 63
Example2:
set = {'a','v','f','z'};
k=3;
Output2:
a v f
a v z
a f z
v f z
in Java plaese.
Thank you!
You should be able to find an appropriate algorithm in D.Knuth's The Art of Computer Programming, Volume 4, fascicle 3 - Generating All Combinations, which can be downloaded from his website.

Categories