I'm working on a school project in Android Studio and I've already asked here how to randomly generate equations, like 10+48*4. Someone suggested me this code to generate the equations:
String[] operationSet = new String[]{"+", "-", "/", "*"};
public void testGenerateRandomEquations() {
Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;
List<String> operations = new ArrayList<>();
for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(3)];
operations.add(operation);
}
int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(Integer.MAX_VALUE) - random.nextInt(Integer.MAX_VALUE);
numbers.add(number);
}
//Now you've the list of random numbers and operations. You can further randomize
//by randomly choosing the number and operation from those list.
}
But now I don't know how to display the generated equation. How can I display the equation for example in a TextView?
Maybe I'm just too dumb to understand but it would be nice if someone could help me :)
Here is the link to the original post: http://stackoverflow.com/a/39960279/6949270
All you have to do is to loop over the two lists - numbers and operations and concatenate them into a single string:
String equation = null;
for (int i = 0; i < numOfOperatrions; i++) {
equation += numbers.get(i);
equation += operations.get(i);
}
equation += numbers.get(numbers.size() -1); //Add the last number to the equation
The last line is needed because there are more numbers (one more) than operations.
It is better to use StringBuilder than String when concatenating strings, but for short strings it's OK.
EDIT - Why does it work?
We are concatenating String with an Integer, using the + operator.
The JAVA compiler converts in this case the Integer into a String, so no other casting is required.
Related
I have an original number (LoadG1). I then produce shuffled versions of this number as seen in the code below. Note: generateG1 is a Random.
int loadG1 = generateG1.nextInt(89999) + 10000;
for (int allrbA = 0; allrbA < 4; allrbA++) {
StringBuilder charLoadG1 = new StringBuilder(String.valueOf(loadG1));
StringBuilder randomLoadG1 = new StringBuilder();
while(charLoadG1.length() != 0) {
int index = generateG1.nextInt(charLoadG1.length());
char c = charLoadG1.charAt(index);
randomLoadG1.append(c);
charLoadG1.deleteCharAt(index);
}
}
if(Integer.valueof(String.valueof(randomLoadG1))==loadG1) {
for (int allrbA = 0; allrbA < 4; allrbA++) {
StringBuilder charLoadG1 = new StringBuilder(String.valueOf(loadG1));
StringBuilder randomLoadG1 = new StringBuilder();
while(charLoadG1.length() != 0) {
int index = generateG1.nextInt(charLoadG1.length());
char c = charLoadG1.charAt(index);
randomLoadG1.append(c);
charLoadG1.deleteCharAt(index);
}
}
This successfully rearranges the numbers in loadG1, seen as the value for randomLoadG1. The issue is, I don't want randomLoadG1 to ever be == to loadG1. This can happen if it is rearranged into the exact same order. I tried using a while loop to sort this out, but it only crashed my app whenever an identical randomLoadG1 was produced.
Can anyone help out with explaining how to get randomLoadG1 (the shuffled version(s) of the original loadG1) to never have the same value as loadG1? Any submitted code is greatly appreciated, many thanks.
Here is a pseudocode implementation:
number1 = random number
do {
number2 = random_shuffle(number1);
} while (number1 == number2)
... where random_shuffle(number1) stands for your existing shuffle algorithm.
Now I don't understand why you can't refactor your existing implementation to follow this pattern, but it should be simple Java programming. If your code is crashing, then you should use a debugger to find out why.
Essentially I'm writing a program that produces random poems out of an array of nouns and an array of adjectives.
This is accomplished basically using this line
String poem = adjectives[rand.nextInt(3)]+" "+ nouns[rand.nextInt(3)];
Simple enough, but I'm supposed to make sure that it doesn't reuse the same noun or adjective for the next poems until all of them have been used at least once already. I'm not sure how to do that.
Convert the arrays to list, so you can use Collections.shuffle to shuffle them. Once shuffled, you can then simply iterate over them. The values will be random order, and all words will be used exactly once. When you reach the end of an array of words, sort it again, and start from the beginning.
If a poem consists of 1 adjective + 1 noun as in your example, then the program could go something like this:
List<String> adjectives = new ArrayList<>(Arrays.asList(adjectivesArr));
List<String> nouns = new ArrayList<>(Arrays.asList(nounsArr));
Collections.shuffle(adjectives);
Collections.shuffle(nouns);
int aindex = 0;
int nindex = 0;
for (int i = 0; i < 100; ++i) {
String poem = adjectives.get(aindex++) + " " + nouns.get(nindex++);
System.out.println(poem);
if (aindex == adjectives.size()) {
aindex = 0;
Collections.shuffle(adjectives);
}
if (nindex == nouns.size()) {
nindex = 0;
Collections.shuffle(nouns);
}
}
The program will work with other number of adjectives and nouns per poem too.
If you must use an array, you can implement your own shuffle method, for example using the Fisher-Yates shuffle algorithm:
private void shuffle(String[] strings) {
Random random = new Random();
for (int i = strings.length - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
String temp = strings[i];
strings[i] = strings[index];
strings[index] = temp;
}
}
And then rewrite with arrays in terms of this helper shuffle function:
shuffle(adjectives);
shuffle(nouns);
int aindex = 0;
int nindex = 0;
for (int i = 0; i < 100; ++i) {
String poem = adjectives[aindex++] + " " + nouns[nindex++];
System.out.println(poem);
if (aindex == adjectives.length) {
aindex = 0;
shuffle(adjectives);
}
if (nindex == nouns.length) {
nindex = 0;
shuffle(nouns);
}
}
What you can do is make two more arrays, filled with boolean values, that correspond to the adjective and noun arrays. You can do something like this
boolean adjectiveUsed = new boolean[adjective.length];
boolean nounUsed = new boolean[noun.length];
int adjIndex, nounIndex;
By default all of the elements are initialized to false. You can then do this
adjIndex = rand.nextInt(3);
nounIndex = rand.nextInt(3);
while (adjectiveUsed[adjIndex])
adjIndex = rand.nextInt(3);
while (nounUsed[nounIndex]);
nounIndex = rand.nextInt(3);
Note, once all of the elements have been used, you must reset the boolean arrays to be filled with false again otherwise the while loops will run forever.
There are lots of good options for this. One is to just have a list of the words in random order that get used one by one and are then refreshed when empty.
private List<String> shuffledNouns = Collections.EMPTY_LIST;
private String getNoun() {
assert nouns.length > 0;
if (shuffledNouns.isEmpty()) {
shuffledNouns = new ArrayList<>(Arrays.asList(nouns));
Collections.shuffle(wordOrder);
}
return shuffledNouns.remove(0);
}
Best way to do this is to create a shuffled queue from each array, and then just start popping off the front of the queues to build your poems. Once the queues are empty you just generate new shuffled queues and start over. Here's a good shuffling algorithm:
https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
How about keeping two lists for the adjectives and nouns? You can use Collections.shuffle() to order them randomly.
import java.util.*;
class PoemGen {
static List<String> nouns = Arrays.asList("ball", "foobar", "dog");
static List<String> adjectives = Arrays.asList("slippery", "undulating", "crunchy");
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println(String.format("\nPoem %d", i));
generatePoem();
}
}
private static void generatePoem() {
Collections.shuffle(nouns);
Collections.shuffle(adjectives);
int nounIndex = nouns.size() - 1;
int adjectiveIndex = adjectives.size() - 1;
while (nounIndex >= 0 && adjectiveIndex >= 0) {
final String poem = adjectives.get(adjectiveIndex--)+" "+ nouns.get(nounIndex--);
System.out.println(poem);
}
}
}
Output:
Poem 0
crunchy dog
slippery ball
undulating foobar
Poem 1
undulating dog
crunchy ball
slippery foobar
Poem 2
slippery ball
crunchy dog
undulating foobar
Assuming you have the same number of noums and adjectives shuffle both arrays and then merge result. you can shuffle the arrays multiple times if you need (once you get to the end)
shuffleArray(adjectives);
shuffleArray(nouns);
for(int i=0;i<3;i++) {
String poem = adjectives[i] + " " + nouns[i];
}
A simple method to shuffle the arrays:
static void shuffleArray( String[] data) {
for (int i = data.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
int aux = data[index];
data[index] = data[i];
data[i] = aux;
}
}
This might be overkill for this specific problem but it's an interesting alternative in my opinion:
You can use a linear congruential generator (LCG) to generate the random numbers instead of using rand.nextInt(3). An LCG gives you a pseudo-random sequence of numbers using this simple formula
nextNumber = (a * x + b) % m
Now comes the interesting part (which makes this work for your problem):
The Hull-Dobell-Theorem states that if your parameters a, b and m fit the following set of rules, the generator will generate every number between 0 and m-1 exactly once before repeating.
The conditions are:
m and the offset c are relatively prime
a - 1 is divisible by all prime factors of m
a - 1 is divisible by 4 if m is divisible by 4
This way you could generate your poems with exactly the same line of code as you currently have but instead just generate the array index with the LCG instead of rand.nextInt. This also means that this solution will give you the best performance, since there is no sorting, shuffling or searching involved.
Thanks for the responses everyone! This helped immeasurably. I am now officially traumatized by the sheer number of ways there are to solve even a simple problem.
I'm a newbie coder and in need some help. Im trying to add the digits of a number together according to a number corresponding to how many digits I can use. Im using Eclipse Juno and using a separate text file for using my numbers. Though it isn't much This is what I have now:
public static void main(String[] args) throws IOException{
String token1 = "";
Scanner infile1 = new Scanner(new File("input.txt"));
ArrayList<String> temps = new ArrayList<String>();
//Adding Input files to the Arraylist
while(infile1.hasNext()){
token1 = infile1.next();
temps.add(token1);
}
infile1.close();
//Calling the Numbers from ArrrayList temps
for(int x = 0; x < temps.size(); x++) {
System.out.println(x + " " + temps.get(x));
for(x = 0; x < temps.size(); x++ ){
}
}
}
}
The numbers are
9678415 7,
9678415 6,
9678415 5,
9678415 4,
2678515 3,
Number to add, digits to use. The input.txt file does not have commas
Exactly how you do this is going to depend on what the data looks like in the file. You say it's not delimited by commas so I will have to assume they are separated by lines. You will need to separate the values within the strings and convert to int; so the below should do what you are attempting, if I understand the question. (full disclosure, it's been a little while since I've written in Java and I don't have a way to test this right now, make sure I haven't made any basic syntax errors)
ArrayList<Integer> totalArray = ArrayList<Integer>();
for(String tStr : temps){
int tempTotal = 0;
String[] numArray = tStr.split(" ");
for(int x = 0; x < Integer.parseInt(numArray[1]){
int y = Integer.parseInt(numArray[0].substring(x,x+1));
tempTotal += y;
}
totalArray.add(tempTotal);
}
There are probably better ways to get the highest value, but since I have been out of this for a little while, I'm just going to do it in the most basic way I can think of.
int highestValue = 0;
for (Integer x : totalArray){
if(highestValue<=x){
highestValue = x;
}
}
return highestValue;
I'm new on this site and, if I'm here it's because I haven't found the answer anywhere on the web and believe me: I've been googling for quite a time but all I could find was how to convert a number to an array not the other way arround.
I'm looking for a simple way or function to convert an int array to an int number. Let me explain for example I have this :
int[] ar = {1, 2, 3};
And I want to have this:
int nbr = 123;
In my head it would look like this (even if I know it's not the right way):
int nbr = ar.toInt(); // I know it's funny
If you have any idea of how I could do that, that'd be awesome.
Start with a result of 0. Loop through all elements of your int array. Multiply the result by 10, then add in the current number from the array. At the end of the loop, you have your result.
Result: 0
Loop 1: Result * 10 => 0, Result + 1 => 1
Loop 2: Result * 10 => 10, Result + 2 => 12
Loop 3: Result * 10 >= 120, Result + 3 => 123
This can be generalized for any base by changing the base from 10 (here) to something else, such as 16 for hexadecimal.
You have to cycle in the array and add the right value.
The right value is the current element in the array multiplied by 10^position.
So: ar[0]*1 + ar[1]*10 + ar[2] *100 + .....
int res=0;
for(int i=0;i<ar.length;i++) {
res=res*10+ar[i];
}
Or
for(int i=0,exp=ar.length-1;i<ar.length;i++,exp--)
res+=ar[i]*Math.pow(10, exp);
First you'll have to convert every number to a string, then concatenate the strings and parse it back into an integer. Here's one implementation:
int arrayToInt(int[] arr)
{
//using a Stringbuilder is much more efficient than just using += on a String.
//if this confuses you, just use a String and write += instead of append.
StringBuilder s = new StringBuilder();
for (int i : arr)
{
s.append(i); //add all the ints to a string
}
return Integer.parseInt(s.toString()); //parse integer out of the string
}
Note that this produce an error if any of the values past the first one in your array as negative, as the minus signs will interfere with the parsing.
This method should work for all positive integers, but if you know that all of the values in the array will only be one digit long (as they are in your example), you can avoid string operations altogether and just use basic math:
int arrayToInt(int[] arr)
{
int result = 0;
//iterate backwards through the array so we start with least significant digits
for (int n = arr.length - 1, i = 1; n >= 0; n --, i *= 10)
{
result += Math.abs(arr[n]) * i;
}
if (arr[0] < 0) //if there's a negative sign in the beginning, flip the sign
{
result = - result;
}
return result;
}
This version won't produce an error if any of the values past the first are negative, but it will produce strange results.
There is no builtin function to do this because the values of an array typically represent distinct numbers, rather than digits in a number.
EDIT:
In response to your comments, try this version to deal with longs:
long arrayToLong(int[] arr)
{
StringBuilder s = new StringBuilder();
for (int i : arr)
{
s.append(i);
}
return Long.parseLong(s.toString());
}
Edit 2:
In response to your second comment:
int[] longToIntArray(long l)
{
String s = String.valueOf(l); //expand number into a string
String token;
int[] result = new int[s.length() / 2];
for (int n = 0; n < s.length()/2; n ++) //loop through and split the string
{
token = s.substring(n*2, (n+2)*2);
result[n] = Integer.parseInt(token); //fill the array with the numbers we parse from the sections
}
return result;
}
yeah you can write the function yourself
int toInt(int[] array) {
int result = 0;
int offset = 1;
for(int i = array.length - 1; i >= 0; i--) {
result += array[i]*offset;
offset *= 10;
}
return result;
}
I think the logic behind it is pretty straight forward. You just run through the array (last element first), and multiply the number with the right power of 10 "to put the number at the right spot". At the end you get the number returned.
int nbr = 0;
for(int i = 0; i < ar.length;i++)
nbr = nbr*10+ar[i];
In the end, you end up with the nbr you want.
For the new array you gave us, try this one. I don't see a way around using some form of String and you are going to have to use a long, not an int.
int [] ar = {2, 15, 14, 10, 15, 21, 18};
long nbr = 0;
double multiplier = 1;
for(int i = ar.length-1; i >=0 ;i--) {
nbr += ar[i] * multiplier;
multiplier = Math.pow(10, String.valueOf(nbr).length());
}
If you really really wanted to avoid String (don't know why), I guess you could use
multiplier = Math.pow(10,(int)(Math.log10(nbr)+1));
which works as long as the last element in the array is not 0.
Use this method, using a long as your input is to large for an int.
long r = 0;
for(int i = 0; i < arr.length; i++)
{
int offset = 10;
if(arr[i] >= 10)
offset = 100;
r = r*offset;
r += arr[i];
}
This checks if the current int is larger than 10 to reset the offset to 100 to get the extra places required. If you include values > 100 you will also need to add extra offset.
Putting this at end of my post due to all the downvotes of Strings...which is a perfectly legitimate answer...OP never asked for the most efficient way to do it just wannted an answer
Loop your array appending to a String each int in the array and then parse the string back to an int
String s = "";
for(int i = 0; i < arr.length; i++)
s += "" + arr[i];
int result = Integer.parseInt(s);
From your comment the number you have is too long for an int, you need to use a long
String s = "";
for(int i = 0; i < arr.length; i++)
s += "" + arr[i];
long result = Long.parseLong(s);
If you can use Java 1.8, stream API makes it very simple:
#Test
public void arrayToNumber() {
int[] array = new int[]{1,2,3,4,5,6};
StringBuilder sb = new StringBuilder();
Arrays.stream(array).forEach(element -> sb.append(element));
assertThat(sb.toString()).isEqualTo("123456");
}
you can do it that way
public static int[] plusOne(int[] digits) {
StringBuilder num= new StringBuilder();
PrimitiveIterator.OfInt primitiveIterator = Arrays.stream(digits)
.iterator();
while (primitiveIterator.hasNext()) {
num.append(primitiveIterator.nextInt());
}
int plusOne=Integer.parseInt(String.valueOf(num))+1;
return Integer.toString(plusOne).chars().map(c -> c-'0').toArray();
}
BE SIMPLE!!!
public static int convertToInteger(int... arr) {
return Integer.parseInt(Arrays.stream(arr)
.mapToObj(String::valueOf)
.collect(Collectors.joining()));
}
this also possible to convert an Integer array to an int array
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i].intValue();
}
Trying to return 5 random numbers between 1 and 42 in Java.
I currently have logic to return a single number (putting it into an ArrayList, but I'd like to do away with that.) I'm stumped on implementation to return 5 random numbers. Would I need 5 for loops?
for (int i = 0; i < 10; i++) {
int r = (int) (Math.random() * 42 + 1);
}
I've seen some other related examples here and they seem more complex than what my needs dictate. However, I could be wrong.
Simply place each random number into an array and return the array...
public int[] powerBalls() {
int[] balls = new int[5];
for (int index = 0; index < 5; index++) {
balls[index] = (int) (Math.random() * 42) + 1;
}
return balls;
}
You can use the Set to generate 5 Unique Random numbers.
Random random = new Random();
Set randomNumbers = new HashSet<Integer>();
while(randomNumbers.size()< 5) {
randomNumbers.add(random.nextInt(42)+1);
}
Since you've mentioned that you're using an ArrayList which will hold all the random numbers, you could just add all the elements present in randomNumbers set to your ArrayList.
Update:-
To suit your needs, you need to do something like this:-
Random random = new Random();
Set<String> set = new HashSet<String>();
while(set.size()< 5) {
set.add(String.valueOf(random.nextInt(42)+1));
}
fortuneList3.addAll(set);
Be careful! Each number can be taken only one time. With your solution it is possible to get same number more than one time.
Other solution (and here you can't have same numer more than one time) is to create array with all numbers, shuffle it and take first 5:
public int[] powerBalls() {
// create array with all numbers
List<Integer> balls = new ArrayList<Integer>(42);
for (int i = 1; i <= 42; i++)
balls.add(i);
// shuffle
Collections.shuffle(balls);
// take first 5
int[] result = new int[5];
for (int i = 0; i < 5; i++)
result[i] = balls.get(i);
return result;
}
Try it like this:
IntArray = new int[5]; //Create an array
for (int i = 0; i < 5; i++) {
IntArray[i] = (int) (Math.random() * 42 + 1);
}
Store the numbers in array and return that array.
int []randArray;
randArray = new int[5];
for (int i = 0; i < 5; i++) { //for 5 random numbers
randArray[i] = (int) (Math.random() * 42 + 1);
}
//now return this array "randArray"
It's a straight forward approach.
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < 5; i++)
{
while(true)
{
int r = (int) (Math.random() * 42 + 1);
if (!generated.contains(r))
{
generated.add(r);
break;
}
}
}
Just throwing my 2 cents in. I recently made a jQuery Plugin, appropriately named "Powerball". I'll share with ya the formula i'm using as well as link ya my plugin. Not sure why anyone would really need this. I did it just for fun! LoL!
The Function
function getNumbers() {
var a=[]; // array to return
for(i=1;5>=i;i++){ // for loop to get first 5 numbers
var b = Math.floor(Math.random()*59)+1; // set #
while (a.indexOf(b) > -1) { b = Math.floor(Math.random()*59)+1; } // reset # if already used
a.push(b); // add number to array
}
a.push(Math.floor(35*Math.random())+1); // add ball 6 number
return a; // 0 index array will have a length of 6, [0-4] being whiteball numbers, [5] being the red ball
}
The Plugin
jsFiddle
Contains the plugin between comment lines
Shows example use
Use is as easy as $("element").powerball(). However, only one method exist for it at the moment, $("element").powerball("setNumbers"). That method simply resets the numbers shown in the p tags.
Style: a note!
All styling is done through a style tag added to the header upon initialization. This means there's no need for extra files to add, but it also gives the ease of custom styling. See more about styling in the jsFiddle!