c strange use of srand and rand c [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 7 years ago.
Improve this question
Can you help me to understand what is the meaning of these randomization?
I have found it in a c code that I have to translate, it return always 41:
int main(){
srand(1);
printf("\n%d",rand());
}
How I can emulate the srand(1) and rand() in Java?

My answer assumes that you just want to emulate the behavior of srand() and rand(), but do not care about the '41' you say that you always get.
This is basically just the idea of pseudo-random number generation. If you set the seed to a constant value (1 in this case), then call the random() function, it will always return the same value. This is because it is basically saying "set the index to 1 in the big list of 'random' numbers" so that the next time I call random it returns the n-th value in that 'list'. In reality, it is a bit more complex, but that's how I like to think about it sometimes. To emulate the behavior of your code in Java, you can try the following:
public static void main(String[] args) {
Random rand = new Random(1);
System.out.println(String.valueOf(rand.nextInt()));
}

Related

Recursions in Java [closed]

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 10 days ago.
This post was edited and submitted for review 10 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm starting with recursion in Java, and I'm having a problem with a counter, where I want to print all numbers from 1 to number, but it only prints the value of i. i was defined at the start of the class as:
int i = 1;
I've tried putting
i>=number
number>=i
i>=1
number>=0
number>=1
number>0
number>1
After getting rid of i, I also tried:
public static void ContadorCreciente(int number) {
if (i <= number) {
System.out.println(i);
ContadorCreciente(i++);
}
}
But now, this is what I've written so far:
public static void ContadorCreciente(int number) {
if (number > 1) {
ContadorDecreciente(number - 1);
System.out.println(number);
}
}
Ok, you're implementing ContadorCreciente with one number argument. Recursive, so you're going to call it again with either number+1 or number-1. For the +1 variant you have no way to stop it, that would keep going up forever, so that's no good, the recursive call has to be with -1.
The -1 recursion can stop e.g. at ContadorCreciente(0) which is expected to do nothing. I'm sure you can figure out how to do nothing and return immediately.
Inductive approach:
Assuming we already have ContadorCreciente(number-1) which prints all numbers 1..number-1. How would you use such a function to implement the case ContadorCreciente(number)?

How to randomly select one from two integers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So lets say I want to randomly choose from the set of two numbers 1 and 3.
How do I go about doing that? Do I just assign int a to 1, int b to 3, and then do randomly select from a and b?
If there are only two numbers to choose from then you can use the value of a boolean, because it returns either true or false.
One-liner solution assuming that int a = 1 and int b = 3:
int randomOfTwoInts = new Random().nextBoolean() ? a : b;
If you have a specific list of numbers then put them in list structure (an array works well). Then you have the easier task of looking for a random index in the range from 0 to last array index. This post lists strategies for doing that:How do I generate random integers within a specific range in Java?

Take a result of two long numbers using an array in Java [closed]

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 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Why Does code.runnable.com Allow Me to Change a Variable's Value in Java? [closed]

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 7 years ago.
Improve this question
I heard that Java integers are pass by value, so why does the following code work in code.runnable.com?
public class HelloWorld {
public static void main(String[] args) {
int number = 0;
number = 2;
System.out.println(number);
}
}
The code will print out 2.
This code snippet doesn't pass number anywhere. You're declaring a local variable and then overriding its initial value. This is perfectly legal in Java, and has nothing to do with passing by reference or by value.

Writing a method using getArray() in Java that returns an array of integers [closed]

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
I am in a a JAVA programming class online and I need some help with making an array. I have asked some fellow class mates to no avail. I need to make write a method using the getArray() method that will return an array of integers with the capacity of 500.
I do know how to write a JAVA statement that initializes an array of integers with a capacity of 500. However, it nots helping me much. (i named the array "values")
int [] values = new int [500];
Please be timely if you answer this. I need to know how to do this before Wednesday 2/5/14 Thank You
public int[] getArray(){
return new int[500];
}

Categories

Resources