Selenium random Numbers need to enter in field box - java

Tried for random numbers by using for loop with count.
I have a field box in that every time it should take random numbers
example: first I enter 1.8$ and run it will accept this number.
after I close the browser and re-run the program it should take the value by its own number but not the previous number mainly it should take decimal values which is greater than 1 & ex: 1.1,1.23 like these numbers only it accepts.

use this method :
public float getDecimalRandomNumber(){
// create instance of Random class
Random rand = new Random();
// Generate and return Random number with decimal
return rand.nextFloat();
}
call it in sendKeys function
driver.findelement(by.id("text box").sendkeys(""+getDecimalRandomNumber());

As you mentioned that the value should not be the previous one, in that case, you should not use any random number. There are chances you will get the same random number. So instead of doing that, you should have to store the state of your variable in a static variable so all the object instances can access the variable and each time increase that number by .1 or by any suitable number. You will not be worrying about the duplication of value.
public class Sample{
private static float inputNumber= 1.1f;
public static float getInputNumber() {
return inputNumber+ 0.1f;
}
}

Related

Does Java Random method return negative numbers?

I'd like to know that if I try to get a random integer using the following method, should it return negative value?
int value = new Random().nextInt(bound);
No, Random().nextInt(bound) only produces positive numbers from 0 to the number you have specified. If you want an negative number, you will need to multiply the random number by -1.
int number = new Random().nextInt(bound) * -1;
Random().nextInt() on the other hand can return you a negative number.
If you need mix of positive and negative you could use something like this:
int number = new Random().nextInt(bound);
if (number % 2 == 0) {
number *= -1;
}
If you use Random class from java.util package you are supposed to mention the type of numbers you expect, meanwhile setting an upperbound. Your answer will be anywhere from 0 to less than upperbound. nextInt() from Random class returns and integer value from 0 to the argument-1. Similarly we can use methods as nextDouble and nextLong(). The values returned are always positive or zero. Now if you need negative values we can randomly set a counter for negative numbers. Say, one another integer value which is randomly generated and checking it is odd/even to negate the number.
The other approach is to use Math.random() method. This method returns a number equal to or greater than 0 and less than 1. We can use typecasting for integer random values else by default we get double values.
P.S. Check the oracle documentation for better understanding of these classes and methods.

Random Class with seed

long seed = 0;
Random rand = new Random(seed);
int rand100 = 0;
for(int i = 0; i < 100; i++)
rand100 = rand.nextInt();
System.out.println(rand100);
I wrote this code to get 100th random integer value of given seed. I want to know if there is a way to get 100th random integer value of given seed without calling nextInt() 100 times.
I want to know if there is a way to get 100-th random integer value of given seed without calling nextInt() 100 times.
No, there is no way to directly get the 100-th random number of the sequence without first generating the other 99 values. That's simply because of how the generation works in Java, the values depend on their previous values.
If you want to go into details, take a look at the source code. The internal seed changes with every call of the next method, using the previous seed:
nextseed = (oldseed * multiplier + addend) & mask;
So in order to get the seed for the 100-th value, you need to know the seed for the 99-th value, which needs the seed for the 98-th value and so on.
However, you can easily get the 100-th value with a more compact statement like
long seed = ...
int amount = 100;
Random rnd = new Random(seed);
// Generate sequence of 100 random values, discard 99 and get the last
int val = rnd.ints(100).skip(amount - 1).findFirst().orElse(-1);
Keep in mind that this still computes all previous values, as explained. It just discards them.
After you have computed that value for the first time, you could just hardcode it into your program. Let's suppose you have tested it and it yields 123. Then, if the seed does not change, the value will always be 123. So you could just do
int val = 123;
The sequences remain the same through multiple instance of the JVM, so the value will always be valid for this seed. Don't know about release cycles though, I think it's allowed for Random to change its behavior through different versions of Java.
Yes. As long as the seed is constant, then the result of executing this 100 times will yield the same result every time. As such, you can just do
int rand100 = -1331702554;
If I got you correct, you search for some seeded method like
int[] giveMeInts(int amount, long seed);
There exists something very similar, the Stream methods of Random (documentation):
long seed = ...
int amount = 100;
Random rnd = new Random(seed);
IntStream values = rnd.ints(amount);
You could collect the stream values in collections like List<Integer> or an int[] array:
List<Integer> values = rnd.ints(amount).collect(Collectors.toList());
int[] values = rnd.ints(amount).toArray();
The methods will use the seed of the Random object, so if fed with the same seed they will always produce the same sequence of values.

The two generate random numbers and their product is different

So im creating a program that generate 2 random numbers and need to multiply them:
public static int thenumber(){
int number1=(int)(Math.random()*10+1);
return number1;
}
public static int thenumber2(){
int number2=(int)(Math.random()*10+1);
return number2;
}
and solve it in :
public static int thefusion(){
int demi =thenumber() * thenumber2();
return demi;
}
My problem is when i run it the product of two number is Different
ex: 7 * 4 = 24
A complete code example would be nice (see How to create a Minimal, Complete, and Verifiable example), but let me guess: You are first seeing the two random numbers (from printing them or some other way). Then you call your method. The method draws two new random numbers from thenumber() and thenumber2(). That’s the point in (pseudo-)random numbers, you don’t the same number each time. So if you drew the numbers 7 and 4 the first time, maybe next time you get 3 and 8, so the product is 24.
There are a couple of possible solutions:
When calling thenumber() and thenumber2(), assign the results to two variables. Now you can see which numbers you got. Pass those two numbers into your thefusion method, and it should calculate the expected product.
Rather than Math.random() use the Random class and instantiate it with a known seed. Draw the two numbers from it and inspect them. Make a new Random instance from the same seed and have thefusion() use it. Now it will draw the same two numbers, and you will get the product you expected.

Random number problems

I have a bit of a complicated question. I'm currently trying to write a REALLY simple version of path finding, and to do that I need a way to generate number within a range, and each number must be different from all the others, and bigger than the last number. How would I go about doing this? So output would look like:
1,5,6,9,15,18
Create a random generator function:
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
(code courtesy from an answer by Greg Case).
Call this wherever you want and check as like:
int a;
a=randInt(min,max);
and for the next time parse the previously generated value, like:
randInt(a, max);
Use reservoir sampling to pick n numbers from your range. Since you're going through the range in order the resulting list is sorted.

Java mistakes with numbers

I am trying to fix the following Java code,
I cannot figure out why the printout is still 5.
public class simpleMath
{
public static void main(String[] args)
{
int number=5;
half(number);
System.out.println("5 divided by 2 is: " + number);
}
private static double half(int number) {
number = number/2;
return number;
}
}
Because you're not re-assigning the returned value.
int number = 5;
number = half(number);
When you call the function, you're discarding its return value:
half(number);
You probably meant to write:
number = half(number);
Also, in Java, arguments are passed by value. This means that, even though you change number inside the function, the change does not propagate back to the caller.
There are several further problems:
Problem 1: The suggested change will store the result in number, which is an integer variable. Thus, the result of half() -- which is of type double -- will be truncated to an integer. To avoid the loss of precision, you either have to change number to be a floating-point variable, or store the result in a different variable of the appropriate type.
Problem 2: The following uses integer division:
number = number/2;
The result is truncated to an integer, i.e. 5 / 2 is 2. The latter is then converted to a double (2.0), which is what the function returns.
To fix, change the function like so:
private static double half(int number) {
return number / 2.0;
}
P.S. Floating-point numbers have a lot of properties that can be unintuitive. I recommend having a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.
You passing the primitive data type which is done by value. You need to give SOP in method half()
EDIT: Need to use the result returned by method half() by either assigning it to number or calling this method in SOP itself.
Why half(number) doesn't modify number declared in main() function? It is because you will pass the value of number to half() function to evaluate, i.e. you give a copy of value in number to half() function. Therefore, whatever half() function does to number will not get reflected back to number variable declared in main(). You need to assign the return value of half() to number in main() if you want to update its value.
There are other cases, such as variable shadowing, that I'm not going to talk in details, since it may confuses you.
It's because you're not assigning the return value of half() to number - it gets calculated but not used.
You need to say:
number = half(number);
The way you have it currently would only work if number was being passed by reference, not by value.
int number =5;
half(number);
Java doesn't support pass by reference. So In this case we are passing value that is 5 not reference of number.
So if we want to capture the changes then method call should be like this -
public class simpleMath
{
public static void main(String[] args)
{
int number =5;
number = half(number);
System.out.println(" 5 divided by 2 is:"+ number);
}
private static double half(int number) {
number = number/2;
return number;
}
}
First you need to be aware of what types you are assigning to your variables. You should change your code to look like this:
public class simpleMath
{
public static void main(String[] args)
{
double number = 5;
double answer = half(number);
System.out.println(" 5 divided by 2 is:"+ answer);
}
private static double half(double number) {
number = number/2.0;
return number;
}
}
See how I now use the returned value and how I divide by 2.0? these changes will give you the results you are looking for.

Categories

Resources