Hey everyone, Im working on a few java programs for a class and this one i can't seem to find an answer anywhere for. I need to write a program that roll's a dice, which is simple, then i have to have it roll the dice 20 times. Also simple. But then i have to show it all as one string of numbers and find the greatest number of same numbers in a row, and put Brackets around them. Here is a actual question.:
-Write a program that generate a sequence of 20 random die tosses and that prints the die values, marking only the longest run.

and marking the longest run is my problem.... any sugestions?

Thanks

What is the 'longest run'?? And if you roll a single die, the max die value you cant get is 6. Im a little bit confused.

Hmm... Here is my guess...

// an example of output
1 3 5 5 2 4 6 6 6 3 3 3 3 4 5 5 1 2 1 2
// then what you need to display is..
1 3 5 5 2 4 6 6 6 [ 3 3 3 3 ] 4 5 5 1 2 1 2

Possible???

int maxRun(){
		int longestRun=-1;
		int maxRoll=0;
		int numOfRolls[]=new int[6];
		int roll;
		//import java.util.Random @ header
		int counter=0;
		while(counter<20){
			Random gen=new Random();
			roll=gen.nextInt(6)+1;
			numOfRolls[roll-1]++;
			counter++;
		}
		for(int i=0;i<6;i++){
			if(numOfRolls[i]>maxRoll){
				longestRun=i+1;
				maxRoll=numOfRolls[i];
			}
		}
		return longestRun;
	}
}

Edited 6 Years Ago by guyfrompluto: n/a

@guyfrompluto: Please also explain your code so that the OP learns how to do it.

@OP: You can create a blank string and then every time you generate a number, add it to the string. Then, at the very end, go through each char (each char=1 roll) and then look for the longest combo. So, something like this:

String s="";
int counter=0;

while(counter<20) {
	int number=//generate random number
	s+=number; //add generated number to s
}

int max=0, current=0, cstart=0, start=0, end=0;
//max=largest length, current=current length, cstart=start of current combo
//start=start of the maximum length combo, end=length of maximum end combo
char c=' '; //can be any arbitrary character not a digit

for(i=0; i<s.length(); i++) { //go through each character of the string
	char tmp=s.charAt(i); //take the character
	
	if(tmp==c) {
		current++;
	}	
	else {
		//reset
		//if current>max, then set max to current, start to cstart and end to i-1
		//then reset current to 1 and c to the current character
	}
}

Edited 6 Years Ago by coil: n/a

I agree with coil's approach - just notice that you need to increment counter at the beginning or you'll end up in an infinite loop. Better make it a for loop since you need to count.

java.util.Random rnd = java.util.Random();
for(int i = 0; i < 20; ++i)
{
  number = rnd.nextInt(6) + 1;
  s += number; 
}

If the long run is what I guess, you can check for the number while each number is generated. You need 2 arrays size 7 and a variable to keep track of the previous generated number and the starting position. Every time a number is generated, check if the new value is equal to the previous value. If it is, increase the value inside subscribe array by 1. If not, check if the value in the subscribe array which the number belongs to is equal to 0. If it is, increment by 1; otherwise, set it to 1 and set the starting position to the number of number generation so far. At the end of the generation, you already know all the run for each number and position...

Edited 6 Years Ago by Taywin: n/a

@coil,

Your code suggestion has multiple issues in searching for long combo. 1)It is ambiguous the way you write it, 2)you declare variables and uses them, but you don't explain where and how to assign value to ALL of them and, 3)the algorithm cannot detect multiple long combos (there are 2 or more number with the same length of combo).

@apines: My bad, yes, a for-loop would have been better.

@Taywin: Could you clarify what you mean for 1) and 2)? And as for 3) from your own post, there is 3 6s and 3 3s, but you only bracketed the 3 3s, so I just assumed you only need to bracket one combo.

@coil,

For 1) I had to reread again to understand that the if statement cover up to its own line. The reason is because there is the word 'then' at the starting of the line next to it. It gives me an impression for if-then which could include both 'then' after the statement.
For 2) You do not reassign cstart anywhere --> would not work unless the cstart is assign to 'i' inside your if statement
For 3) Please look at my example again. How many 3s and 6s in its combo?

Edited 6 Years Ago by Taywin: n/a

1) Oops, that was bad commenting on my part, but no, it wasn't my intention to create an if-then structure.
2) Well I'm assigning start to cstart (the start of the combo)...
3) Never mind, misread.

In any event, I think we should wait for the OP to come back first and try some of the suggestions.

Edited 6 Years Ago by coil: n/a

i am also learning Java now, and faced the very same question as you
i did manage to solve this question, however the syntax is not considered good
here is my solution(still there is a bug, when there is no "run", the brackets will appear behind the first number)

import java.util.Random;
public class Run
{
    public static void main(String args[])
    {
        int[] dices = new int[21];
        String str = "";
        Random rand = new Random();
        boolean inRun = false;
        for (int i = 0;i<20;i++)
        {
            dices[i] = rand.nextInt(6)+1;
        }
        for (int i =0;i<20;i++)
        {
                str = str +dices[i];
        }
        System.out.println(str);
        
        int count = 0;
        int maxcount = 0;
        int endPosition= 0;
        boolean inMax = false;
        for (int i = 0;i<20;i++)
        {
            if (dices[i]==dices[i+1])
            {
                count++;
                if(count >= maxcount)
                {
                    maxcount = count+1;
                    inMax = true;
                }
            }
            if (dices[i]!=dices[i+1])
            {
                if(inMax)
                {
                    endPosition = i;
                    inMax = false;
                }
                count=0;
                
            }
        }
        int startPosition= endPosition - maxcount+1;
        System.out.println(str.substring(0,startPosition)+"("+str.substring(startPosition,endPosition+1)+")"+str.substring(endPosition+1,20));
        
    }
}
This article has been dead for over six months. Start a new discussion instead.