Random.nextInt seems wrong from a book [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
It's a block of code for random verification code in a Servlet book.
the underlined part must be a number of 30+?
but the intention is one of those chars above
Pic
NextInt method only has one parameter which is the max number (exclusive)?
So is it a mistake in the book?

nextInt(int) returns a [pseudo]random number between 0 and the parameter passed to it, exclusive. Thus, if you pass a length of an array, you'd get a random valid index from the array, which you can then use to pick a random element from it, as this code does.

Related

1e5 which is equivalent to 10^5 . Is this value represents double or int in java. Can You please elaborate the concept [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
This Code is giving error that lossy conversion from double to int.
int[] ans=new int[1e5+1];
The concept is simple: 1E5 is a 'double' floating-point number in Java because the Java language specification says it is.
Meanwhile, an array size must be integral, and the compiler is telling you that. If you really want to use a floating-point number there, you need to cast to int. However, it's easier to just write 100000.

Can you make if/else statements for when rand.nextInt() = 0? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is a segment of the code for my program. What I'm trying to do is match the health of the enemy with the random enemy that get selected. My plan is to make a condition for when the randomInt selects a number so I can print out the correct enemyHealth value from my array. Is there any way I can set the a condition for when rand.nextInt is equal to some number?
if(rand.nextInt(enemy.enemyType) = 0)
System.out.println(enemy.enemyHealth[0]);
if(rand.nextInt(enemy.enemyType) == 0)
System.out.println(enemy.enemyHealth[0]);
= is an assigning operator. It assigns a variable a value and that is not a variable. == is a comparing operator for primitive type variables.

Java Random Number with seed changes after a day [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I was using the code below to generate random number with seed so that I can generate back the same results next time.
int seed = 100;
Random rand = new Random(seed);
I manage to get back the same results from the program on the same day, but after a day or two i get a completely different results. Is there a problem of the way I implement the random seed? Anyone encounter this before?
Citing from Javadoc:
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.
It really depends on how you use that instance. It has to be strictly the same sequence of methods you call.
This means the problem is in the code that calls the methods on your Random instance. The sequence of the methods called seems to depend on some conditional that can change it.

Why does containsAll return false? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does this code evaluate to false?
code:
String[] a = {"donald,duck"};
String[] b = {"duck,donald"};
System.out.println(Arrays.asList(a).containsAll(Arrays.asList(b)));
output:
false
From the docs:
boolean containsAll(Collection c)
Returns true if this list contains all of the elements of the specified collection.
Update: Realized the flaw as soon as the first answer ticked in. I'll go and sit in the corner for a while now, thanks. *equips hat of shame*
Since "donald,duck".equals("duck,donald") is false, hence the result. You've 2 arrays with 1 elements each.

Illegal format string in TextIO.putf() method aka wrong Array Type? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I guess its a rather simple question but i just cant find my mistake.
int[] myIntArray = new int[20];
myIntArray[5] = 5;
int a = myIntArray[5];
TextIO.putf("arr[i]: d%",a );
The error I get is Illegal format string in TextIO.putf() method.
So I assume the value at the index 5 is not an int?
The error message says exactly what the problem is: your format string is wrong. You probably meant %d (or better yet, %d%n to add a newline).
The format string elements are in the form
%[modifiers]type
not
something%
Change d% to %d.

Categories