How to fill a text area with random numbers [closed] - java

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 9 years ago.
Improve this question
I have a Java application and want to press a button to generate a 7 digit random number and put it into a text area.
Here is what I have so far:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText();
}
How do I do this?

i don't know what to write inside the brackets to get this random number of seven digits on the text area.
You can use a StringBuilder and append 7 random numbers between 0 and 9 using the nextInt(int n) method :
Random r = new Random();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 7; i++)
sb.append(r.nextInt(10));
jTextArea1.setText(sb.toString());

first you'll need to inport the java random number library at the top of your code, like this:
import java.util.Random;
this code will get you a random number from 1000000 to 9999999, it is a little weird, but take some time trying to figure it out
1000000 + (int)(Math.random() * ((8999999) + 1))
Try putting that between the parenthasis after setText, like this:
jTextArea1.setText(1000000 + (int)(Math.random() * ((8999999) + 1)));
For more information about the random function and how it works, looks here

Related

Filling String list with a recursive method ( Recursive fill method ) [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 2 years ago.
Improve this question
I want to write a recursive method that fills and returns a String[] list of random balls for either "blue" or "red" of length n where n is a random odd number.
I wrote this method to generate an odd number in range 1-10 to use as a parameter for the recursive method.
public int ranNum (int ranN) {
int min = 1
int max = 10
Random r = new Random();
ranN = min + r.nextInt((max - min)/2)*2;
return ranN;
}
Is the (random odd number) method right? How to implement the recursive method?
A recursive function is a function which calls itself directly or via any amount of intermediate function calls.
The first step usually is to think about the termination. In your case you want to either count from 1 up to n or from n down to 1. And when the function terminates you have the choice of either returning the final value (it's called tail-recursion) or start building up your answer by traversing the call stack upwards. The later is being used, when you have one long calculation and you just want the last value (e.g. Fibonacci-Numbers).
And then do the work in every step:
String[] tailRecursion(int n, String[] accumulator, Random rnd)
{
if(n == 0) {
return accumulator;
}
accumulator[n-1] = rnd.nextBoolean() ? "red" : "blue";
return tailRecursion(n-1, accumulator, rnd);
}
The method is called via tailRecursion(10, new String[10], new Random())

java code to Divides number and show the reminder and quotient [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
I hope this thread isnt against the rules
my problem is
im using macos and i couldnt find a calculator that Divides number and show the reminder and quotient
so i thought there might be a java code that do that
i could program such app but im really on a hurry
couze im studing multiplicate cipher that uses gcd (greatest common diviser )
and i need to culaculate alot of number using the reminder of that calculation
so i thought i bring this here cause there is alot of experienced programs in this form
thanks in advance
I have found the code
//Author : Mayank Rajoria
import java.util.*;
class remainderAndQuotient {
public static void main(String[] args) {
int n, q, a, k = 1, r;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers");
n = sc.nextInt();
a = sc.nextInt();
r = n % a;
q = n / a;
System.out.println("Quotient : " + q);
System.out.println("Remainder : " + r);
}
}

generate random objects to get spawning for java [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
I'm trying to generate a random double for this section of my assignment.
The question is
"Each week, each female Guppy who is 10 weeks old or older has a 25 percent chance of spawning".
I would like to generate a random double to determine if each female Guppy should spawn or not.
My code so far:
Random r = new Random();
if (isFemale == true && getAgeInWeeks() >= 10) {
//?
}
I don't see any reason to generate a random double according to your question. What you need is an integer ranging from 0 to 3 inclusive where each number account for 25% for the spawning.
Random r = new Random();
if (isFemale == true && getAgeInWeeks() >= 10) {
// Generate a random integer in [0,3]
// Since there is a 25% or 1/4 chance
if(Math.abs(r.nextInt()) % 4 == 1){
//Note that the 1 in the condition can be replaced by any
// integer in [0,3]
//Put spawning code here
}
}
Check out this link for more information on random:
Random (Java Platform SE 7
To generate a random double, you can look at this question, however, this problem can more easily be solved by generating random ints.
In your situation, generating an int between 0 to 3 is what you want because checking if it is 0 will be true 25% of the time (1 Value / 4 Possible values = 25%).
EDIT: If you would like to also generate a random number to see how many spawn a Guppy will have use threadLocalRandomInstance.nextInt(int bound); like before.
These constraints can be translated to code like this:
import java.util.concurrent.ThreadLocalRandom;
public class Test {
public static void main(String[] args) {
ThreadLocalRandom tlr = ThreadLocalRandom.current();
int num = tlr.nextInt(3 + 1); //Bound is exclusive so add 1.
int spawn;
if(num == 0) {
spawn = tlr.nextInt(100 + 1); //Again, bound is exclusive so add 1.
} else spawn = 0;
System.out.println("This guppy had " + spawn + " spawn.");
}
}
I use ThreadLocalRandom as it is more straightforward as supported by this answer.
If you are not using Java 1.7+, use Random#nextInt(int) instead as also shown by that answer.

Can anyone please explain to me the following Java code? [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 7 years ago.
Improve this question
The following code shuffles playing cards in a game. I have to explain what the code does in a presentation. I am a newbie in Java coding, so can anyone please explain to me the following code line by line:
// Deck shuffling method
public void shuffleDeck() {
//Seed the Random instance with nanoTime
Random random = new Random(System.nanoTime());
for(int i = 0; i < 52; i++) {
int swapIndex = random.nextInt(52);
if (swapIndex != i) {
PlayingCard temp = cardDeckArray[i];
cardDeckArray[i] = cardDeckArray[swapIndex];
cardDeckArray[swapIndex] = temp;
}
}
cardIndex = 0; //Next card to be pulled off the deck
}
This is a shuffle of a deck, at each iteration you swap two cards at random, and it will result in some random permutation of the deck.
However, this algorithm is flawed and biased, there are some permutations that are more likely to be generated than others, to use an unbiased shuffle, you should use fisher-yates shuffle (which is basically the same idea, but generate a random number between i to 52, instead between 0 to 52)
The reason and results of this bias is discussed thoroughly in the thread: What distribution do you get from this broken random shuffle?

How do I insert data of a for loop in a specific index? [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 have a "for" loop that is creating a variable each time through the loop. I am attempting to insert the results into an empty array at the index of the "i" in the loop. From the best I can tell it seems I need to create a ArrayList vs. an Array to make this happen.
int varNum = 10;
Array someArr = new Array ();
for (int i = 0; i < 10; i++){
varNum = varNum +i;
someArr[i] = varNum;
}
On the first loop I want the 10 to be inserted in my array at the "0 index", 11 inserted at "1 index", 12 at the "2 Index".
**The important part is that the Array is not a set size, because I do not know how many indexes I will need in the array, so I want to add them as I needed.
If you use an ArrayList you can call add like this:
int varNum = 10;
ArrayList<Integer> someArr = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
varNum = varNum + i;
someArr.add(varNum);
}
This will allow you to dynamically fill the ArrayList dependent upon how many values it needs to hold.
Better use an ArrayList then, and use their .add() method
someArr.add(varNum)

Categories