FOR THOSE CONFUSED ABOUT ++freq[1 + rand.nextInt(6)]: we have this: ++freq[1 + rand.nextInt(6)]. Java will use 1 + rand.nextInt(6) to pick a random number between 1 and 6. lets say 4 is the number that is randomly chosen. java will see that in the array as ++freq[1 + 4]. freq[5] is the 6th index in an array (dont forget that your computer starts counting from 0). the ++ part will simply add one to the number in that index. in bucky's case, we have an empty array with 7 indexes. each index equals zero by default. so if ++freq[1 + rand.nextInt(6)] is writen, java will pick a random number between 1 and 6 (lets say 2 this time), and add one to the number within the index. since freq[2] = 0, we say ++freq[2] so that freq[2] now equals one. does anyone still have no idea how that works?
+Purushoth Purushoth absolutely so we have the face and frequency. the face is the side of our "dice" that will show when we roll it. the frequency is how many times that face shows up after we roll it x amount of times, in this case x = 1000. so the frequency will be much higher, in the hundreds range. if he use 100 or 10, you'd see smaller numbers in the frequency column any other questions? I'm glad to help!
for ease of use, in eclipse, you can just start using random, you will get errors, but typing Ctrl+shift+o will automatically insert the appropriate import statement at the top. also, instead of typing System.out.println, you can type Sysout and then type Ctrl+space and eclipse will type the rest for you
netbeans is even better in my opinion. to import, hover over the error and press Alt+Enter, and if you type sout+Tab, you get a print line statement by default
I realize Bucky was a couple of steps ahead of his audience on this tutorial but i hope this breaks it down for y'all: ///////////////////////Replace the line///////////////////////: ++freq[1 + rand.nextInt(6)]; with int count = 1 + rand.nextInt(6); freq[count] = freq[count] + 1; The last line above can be written in many ways. It just means add 1 to it. These lines below mean the same: i + + + + i i + = 1 i = i + 1 i = 1 + i Note: i is synonymous to freq[count] in this example
Took me a while to understand why he was doing ++freq. What helped me understand what was going on was to change the line that said 1+randomizer.nextInt(6) to 5. So you will have ++freq[5]. Then once you run the program, the entire values would be 0 for all of the indexes EXCEPT freq[5], which was equal to 999. To make things short, 1+randomizer.nextInt(6) will always land on a number from 1-6. For example, let's say 1+randomizer.nextInt(6) lands on 3. So you have ++freq[3]. Now the index freq[3]'s value will be one. If it lands on freq[5], then it will add one to the value of freq[5]. Do what I said in the first paragraph to help you understand. It will all fall into place.
Instead of skipping the 0th element of the array, I find it easier to just increment the "face" value by 1 when printing out the results at the end of the program. That way you can declare the arrays to have 6 elements (the same as the number of possible dice rolls) and things are a bit less confusing. import java.util.Random; public class apples{ public static void main(String args[]){ Random rand = new Random(); int freq[] = new int[6]; for(int i = 0; i < 100000; i++){ freq[rand.nextInt(6)]++; } System.out.println("Roll\tFrequency"); System.out.println("----\t---------"); for(int face = 0; face < freq.length; face++){ System.out.println((face + 1) + "\t" + freq[face]); } } }
You "Don't" have to create 7 arrays of int. Actually you can use 6. Then the changes are: ++freq[rand.nextInt(6)] int face = 0; in the for loop. and in the last System.out.println((face+1)+"\t"+freq[face]);
Good point Svetan Dimoff from below comments! Before any dice rolls we have: freq[0] = 0 freq[1] = 0 freq[2] = 0 freq[3] = 0 freq[4] = 0 freq[5] = 0 freq[6] = 0 So let's say we roll a dice for the 1st time and it hits 3 (3 dots on the physical dice), then our Array becomes freq[0] = 0 freq[1] = 0 freq[2] = 0 freq[3] = 1
when there is no space after the equals sign it really messes with me. I must have OCD or something because I cringe when I see no space after a equals sign.
this is a good example to understand concept of array. the take away from here is, that you can increment the value for an index using ++freq (in this case). It is very useful in actual programs where you need to keep a count of how many times a specific value is repeating (group function).
Thank you. My program was only showing one roll, with no errors. I checked the code in the forums and found an extra ; I will never be able to thank you enough for these videos!
@Quentello \t means tab, the backslash is an escape character that tells java that the next character isnt part of what is inside the " " so it doesnt print the t, and also does something about it, there is a list of what each character does, for example a would skip a line, you can try it out.
Wow, this took me so long to understand. Basically the random number generator generates a number between 0 to 5. We counter this by adding +1. Whatever number is then generated is what the index is. The array then looks at the index, finds the number, and then adds 1 to it, via the ++freq part. This is how it stores the numbers in the array
Hi guys.Pretty understandable this tutorial ,however, could anyone explain me what is the difference of putting ''freq[1+rand.nextInt(6)]++'' instead of ''++freq[1+rand.nextInt(6)]'' ?
pre-incrementing changes the value right away, so if a=5, print(++a) will give 6, post-incrementing changes the value at the second time you use it, so print(a++) will give 5, but the second time if you print(a), it will give 6.
Both are quite similar but using ++a is a safer option since it increments the value immediately as soon as it encounters a face of the die.... but a++ increments the value of the slot after the statement gets executed once, in this case since it's a thousand rolls it's so difficult to spot, but better to use ++a in this scenario.
For anyone who is confused with how the for loop works here's an explanation. roll starts at 1 (int roll = 1;) this declares an int called roll and initialises it to hold a value of 1. This only happens once in this program. The program now looks at the second part of the for loop (roll < 1000) as roll holds a value of 1 and 1 < 1000 is true the program now enters the for loop. rand is the name of his Random object and he uses one of Random's methods called nextInt(int) which will return
Took a minute to wrap my head around that one too. What I think it says is that ++freq[3] would have been ++freq[1+2]. This is to avoid placing any values in the 0 element (array position 0). So yes this would place it into array position 2. Face on the other hand starts at 1 so therefore it would be placed in face 3. Remember, array starts at 0, Face starts at 1. Hope this helps.
After looking at this a few hours ago then coming back to the code I copied in my IDE, I finally have a better understanding as to why this works. I was so mad at myself because it seemed so easy but I couldn't grasp why the code worked. For me, the main thing that was throwing me off was the ++freq[1+rand.nextInt(6)]. What made it click for me was putting freq[1+rand.nextInt(6)] += 1; in my IDE just to see it work that way. I put comments by a few lines of code to explain it to myself in my IDE. :D
Hey guys who dont understand this one: ++freq[1+rand.nextInt(6)]; You just adding one into random array 1-6 each round (for cyklus) you add one into just one of this 6 arrays. To understand better copy this code into your main: Random rand = new Random(); int Sebarray[] = new int[5] ; int counter=1; for (int roll=1;roll
DON'T think you're stupid for not getting the code right away, Bucky is to blame for not expanding the array indexes like that, : freq[0] = 0 freq[1] = 0 freq[2] = 0 freq[3] = 0 freq[4] = 0 freq[5] = 0 freq[6] = 0 Then ++freq would have made sense right away. See, anytime you land a number, you just tick one at the zeroes on the right side.
If anyone is confused i would suggest going and watching not just one of these tutorials (one "series") but watching one series until it gets too confusing because you don't really know/understand what was taught in the previous tutorials so instead of going back and watching them again the old ones which you probably wouldn't pay attention to anyway since you have already seen it but instead go to a different language playlist and solidify your understanding of the basics / previous material.
I am learning this for my new job and the explanations that you use are brilliant! I didn't get the ++freq[1+rand.nextInt(6)] in the beginning but now... keep up the awesome vids!
To those who don't understand how the array is adding 1 to the value: freq[] = 1+rand.nextInt(6); can be rewritten as int test = 1+rand.nextInt(6); freq[test] = 1 + freq[test]; I make a new integer equal to 1 + a random number between 0-5 I set the array position for that random number equal to 1 more than it was before. So for example: If the random number generator gives us a two, we add 1 to it, getting 3. We make the number assigned to three in the array equal to 1 + what ever it was before. Hope that helps.
So, just for the fun of it I wanted to make a program that would also show me the individual rolls and not just the frequency of the results, and gave me a way to choose how many dice I wanted to roll. I struggled a bit but finally: import java.util.Random; import java.util.Scanner; class test3 { public static void main(String[] args){ Random rand= new Random(); int num; int freq[]=new int[7];
Scanner in = new Scanner(System.in); int dice; System.out.println("How many dice? "); dice = in.nextInt(); in.close();
@kotb90 the part with "1+rand.nextInt (6)"will give you a random number between 1 and 6 (see tutorial 29) so, for example the random will give you ++freq[4]. remember that if you have variable "bucky" and you want to add 1 to the current value, you could do ++bucky to do so. so when we do ++freq[4] we take the current value of freq[4], and add 1 to it. that way we count how many time the random finds "4" as an answer.
make your program roll the dice 1,000,000 (million time) have to wait about 10 to 20 seconds for it to catch it was very fun finding the output all roughly the same value. Great tutorials thanks a lot for all your hard work!
for those who didn't understand the ++freq[index] , I have done bucky's code in a more understandable way that if you compare them you will definitely get the point. Hopefully, it would be helpful. you just need to create a test class test.java in copy paste the code blow. import java.util.Random; import java.util.Scanner; public class test { public static void main(String[] args){ Scanner input = new Scanner(System.in); Random dice = new Random(); int roll, rndNumber; int freq[] = new int[7]; int freq2[] = new int[7]; System.out.println("how many random number: "); roll = input.nextInt(); for(int i = 0; i < roll; i++){ rndNumber = 1+dice.nextInt(6); if(rndNumber == 1){ freq[1] += 1; }else if(rndNumber == 2){ freq[2] += 1; }else if(rndNumber == 3){ freq[3] += 1; }else if(rndNumber == 4){ freq[4] += 1; }else if(rndNumber == 5){ freq[5] += 1; }else if(rndNumber == 6){ freq[6] += 1; } ++freq2[rndNumber]; } for(int face = 1; face < freq.length; face++){ System.out.println(face + "\t" + freq[face]); } System.out.println("-----------------"); for(int face = 1; face < freq.length; face++){ System.out.println(face + "\t" + freq2[face]); } } }
Alright, thanks for your quick reply! I'll try that out. Although I still am wondering why it doesn't work properly. But good to know that (in theory) I wasn't (all that) wrong :)
Where you place the ++ just decides whether its a pre-increment or a post-increment. What that means is that pre-incrementing adds 1 BEFORE the rest of that line is ran, and post-incrementing adds 1 AFTER the code on that line was ran. So, in this circumstance, the effect of that change in the code will not affect the outcome.
ok im just basing this off my understanding of data structures in general but I ran this code with 1 rather than 100 and such and it supports my interpretation: the freq[] is just calling a random element of the list (there being 6 elements, or "slots" to chose from). the ++ just adds one to that random element, which is a value of 1-6 for the 6 available slots- the random number generator is just giving a random index- if you wanted a 12 sided die youd type freq[1 + rand.nextInt(12)]
-Any random number was generated by rand which saved in freq[] array. -face is just temporary variable. You can change it as whatever kind different you want. Its function is show index of array freq[].
Java is nearly as fast as C++ now. It used to be a lot slower than C++ but now it has caught right up in speed. Little tip for anyone out there who wants to become a programmer. Don't become a poster boy for a certain language. You may think that one language is better than the rest which will mainly be due to your knowledge and enjoyment of that language but every programming language has its uses.
This outputs the same result and may be less confusing to understand. To get the values in the index to 1-6 you simply add 1 to the face in the System.out.println code. Also to get 1000 rolls, you can either change the roll
++freq[random number between 1 & 6 inclusive] adds 1 to the value at that index. Remember we access arrays through index numbers. freq[0] would allow us access to the first value in the array. Because they aren't initialized to begin with they are all 0 right now. If our random number generator rolls a 6 (freq[6]) it will add 1 to that, so the value at the index freq[6] = 1. If we get six again, freq[6] = 2. I will keep generating random numbers and adding 1 to an element in the array (index)
Okay guys, to learn this try to really break it down. What I mean by this is instead of looking at '++freq[1+rand.nextInt(6)];' Try to first identify what the random generator does: '++freq[rand.nextInt(6)];' Then add the +1: '++freq[1+rand.nextInt(6)];' That's how I learnt
Instead of wasting an array slot for face zero, we can just output face+1, and drop the 1+ in the random for loop. The full code is below, from another user.
A general advice for everyone, try to take sleeping rest every 10 episodes and keep thinking in your head of all the codes you learned. Now I can do almost all the previous 29 tutorials from my mind. FFS I dream about java now.. WTF! Lmao
A really minor exercise for who ever wants. tell the program to say , what's the total times you've rolled the dice. It taught me few new concepts I didn't know , important onces.
The first for loop was explained by Toadfoster (Look at top comments) Remember that in the for loop, it goes for(starting point; end point; increment) So the starting was 1, and ending LESS than 7 (Length of Array) but NOT EQUAL to 7, so we get exactly 6 'tables' Then we print out the 'face' number which goes under 'Roll' which starts at 1, ends at 6. The freq[face] will print all freq[1] freq[2]...freq[6] values, that were assigned in the first for loop. Ask if you need any further explanation
Constantine, I think Bucky uses ++freq[] instead of freq[]++ because he wants to first calculate the new array index number and then increment the value - as Tamerlan said. Using Tamerlan's example on the first loop with freq[]++, the result would be to increment freq[0] by 1 and then calculate the new index value of 3.
If you guys don't want to use eclipse you can use BlueJ its pretty good for beginners because it color codes the classes and methods, but I would recommend eclipse.
a much easier way to do the random number, where you don't have to import anything is just type: (int)(Math.random()*6+1); This does the same thing but much quicker. The "int" rounds off the number to be a whole number, and the +1 again is because Math.random is a random number bigger then 0 and less than 1.
int freq[] has 7 elements and the value of each element is set zero by default. e.g. ++freq[1+rand.nextInt(6)] => ++freq[3] => freq[3] = 0+1 or freq[3] = zero initially and it is being incremented by 1. In the output there is a frequency of element 3 or freq[3]
so from what've understood, [ 1+rand.nextInt(6)] generates the array indexes from 1 to 6. the ++ freq adds the occurances of similar indexes. In the beginning all the freq[indexNr] are equalto zero since the rolls havent happened yet roll 1--> index=2,-->freq[2] =1 (++freq[2]--> freq[2] =freq[2]+1 or occuranceOf2= 0+1 becomes 1occurances of freq[2]) roll 2 --> index=4 -->freq[4]=1 (++freq[4]--> freq[4] =freq[4]+1 or occuranceOf4= 0+1 becomes 1 occurances of freq[4] ) roll 3-->-index= 2-->freq[2] =2 (++freq[2]--> freq[2] =freq[2]+1 or occuranceOf2= 1+1 becomes 2 occurances of freq[2]) roll4-->index =5 --> freq[5]=1 (++freq[5]--> freq[5] =freq[5]+1 or occuranceOf5= 0+1 becomes 1occurances of freq[5]) roll5 -->index=2--> freq[2]=3 (++freq[2]--> freq[2] =freq[2]+1 or occuranceOf2= 2+1 becomes 3 occurances of freq[2]) roll 6-->index =4--> freq[4]=2 (++freq[4]--> freq[4] =freq[4]+1 or occuranceOf4= 1+1 becomes 2 occurances of freq[4]) etc After this loop comes the print statement where the number of the dice is printed next to the total sum of each of the index occurancesnof the array, so basically the faces of the dice are the index numbers of the array. Hopefully this helps a bit
@Soulscient Nope. 1+random adds one on every roll. 1+freq adds one to the value of the array on the position freq[1+rand.nextInt(6)], that is the number of roll in which you got that value.
The ++freq bit means add 1 to the value that is stored in freq[1+rand.nextInt(6)] so if 1 + rand.nextInt(6) comes out at 3. The number of times a 3 would have been rolled will need to increase by 1 which is what ++ does. Hope someone found that helpful in some way.
I love this community, Finally a comment section that is actually useful :)
cant get that on so
Yas it is true betch
FOR THOSE CONFUSED ABOUT ++freq[1 + rand.nextInt(6)]:
we have this: ++freq[1 + rand.nextInt(6)]. Java will use 1 + rand.nextInt(6) to pick a random number between 1 and 6. lets say 4 is the number that is randomly chosen. java will see that in the array as ++freq[1 + 4]. freq[5] is the 6th index in an array (dont forget that your computer starts counting from 0). the ++ part will simply add one to the number in that index.
in bucky's case, we have an empty array with 7 indexes. each index equals zero by default. so if ++freq[1 + rand.nextInt(6)] is writen, java will pick a random number between 1 and 6 (lets say 2 this time), and add one to the number within the index. since freq[2] = 0, we say ++freq[2] so that freq[2] now equals one.
does anyone still have no idea how that works?
+Kenneth Fleming thanks for the explanation sir :)
sir can you explain why it is printing a 3 digit number in frequency
+Purushoth Purushoth absolutely
so we have the face and frequency. the face is the side of our "dice" that will show when we roll it. the frequency is how many times that face shows up after we roll it x amount of times, in this case x = 1000. so the frequency will be much higher, in the hundreds range. if he use 100 or 10, you'd see smaller numbers in the frequency column
any other questions? I'm glad to help!
Will we get same results if we post increment the frequency ??? ( I mean freq[1 + rand.nextInt(6)]++) only for this particular Dice problem.......
+ashai reddy yes, it should work both ways
I didn't understand this program at first, leaving and coming back later with a clear mind made it easier to understand.
for ease of use, in eclipse, you can just start using random, you will get errors, but typing Ctrl+shift+o will automatically insert the appropriate import statement at the top. also, instead of typing System.out.println, you can type Sysout and then type Ctrl+space and eclipse will type the rest for you
netbeans is even better in my opinion. to import, hover over the error and press Alt+Enter, and if you type sout+Tab, you get a print line statement by default
I realize Bucky was a couple of steps ahead of his audience on this tutorial but i hope this breaks it down for y'all:
///////////////////////Replace the line///////////////////////:
++freq[1 + rand.nextInt(6)];
with
int count = 1 + rand.nextInt(6);
freq[count] = freq[count] + 1;
The last line above can be written in many ways. It just means add 1 to it. These lines below mean the same:
i + +
+ + i
i + = 1
i = i + 1
i = 1 + i
Note: i is synonymous to freq[count] in this example
Took me a while to understand why he was doing ++freq. What helped me understand what was going on was to change the line that said 1+randomizer.nextInt(6) to 5. So you will have ++freq[5]. Then once you run the program, the entire values would be 0 for all of the indexes EXCEPT freq[5], which was equal to 999.
To make things short, 1+randomizer.nextInt(6) will always land on a number from 1-6. For example, let's say 1+randomizer.nextInt(6) lands on 3. So you have ++freq[3]. Now the index freq[3]'s value will be one. If it lands on freq[5], then it will add one to the value of freq[5]. Do what I said in the first paragraph to help you understand. It will all fall into place.
Array[1+object.nextInt(6)] += 1;
This line of code is far better and easy to read imo
I can read the text over and over and still not understand but when I listen to your video I get it the first time. Thank you!!
Instead of skipping the 0th element of the array, I find it easier to just increment the "face" value by 1 when printing out the results at the end of the program. That way you can declare the arrays to have 6 elements (the same as the number of possible dice rolls) and things are a bit less confusing.
import java.util.Random;
public class apples{
public static void main(String args[]){
Random rand = new Random();
int freq[] = new int[6];
for(int i = 0; i < 100000; i++){
freq[rand.nextInt(6)]++;
}
System.out.println("Roll\tFrequency");
System.out.println("----\t---------");
for(int face = 0; face < freq.length; face++){
System.out.println((face + 1) + "\t" + freq[face]);
}
}
}
hi guys i am new in youtube and i am making java tutiorals i would appreciate if you subed thanks
These tutorials are so well explained. I love 'em! Wanted to point out that the dice is rolled 999 times with "for(int roll=1;roll
You "Don't" have to create 7 arrays of int. Actually you can use 6.
Then the changes are: ++freq[rand.nextInt(6)]
int face = 0; in the for loop.
and in the last System.out.println((face+1)+"\t"+freq[face]);
Good point Svetan Dimoff from below comments! Before any dice rolls we have:
freq[0] = 0
freq[1] = 0
freq[2] = 0
freq[3] = 0
freq[4] = 0
freq[5] = 0
freq[6] = 0
So let's say we roll a dice for the 1st time and it hits 3 (3 dots on the physical dice), then our Array becomes
freq[0] = 0
freq[1] = 0
freq[2] = 0
freq[3] = 1
when there is no space after the equals sign it really messes with me. I must have OCD or something because I cringe when I see no space after a equals sign.
this is a good example to understand concept of array. the take away from here is, that you can increment the value for an index using ++freq (in this case). It is very useful in actual programs where you need to keep a count of how many times a specific value is repeating (group function).
When I first saw this, I think just about all my brain fuses burnt out...
lol, just wait until you get into polymorphism and inheritance then.
hi guys i am new in youtube and i am making java tutiorals i would appreciate if you subed thanks
i just don't understand..i came here for sum of arrays of floating point numbers..
i cant believe i took this mess
@@codeisfun7272 no
Thank you. My program was only showing one roll, with no errors. I checked the code in the forums and found an extra ; I will never be able to thank you enough for these videos!
0:16 Hilarious, good vids man!
Thank you so much for this. I love the way you break down every part of the code just in case someone is confused about a certain part. Very helpful.
CONGRATS EVERYONE YOU MADE IT TO THE 30TH VID ON JAVA. Give a pat on the back you got this!
This was probably the hardest video ive watched you made but when you explained in the end i finnaly understood
You're the best Bucky ! If I get trough this exam it will be because of you mang
+Arveer Singh No I'm a Belgian guy :D grts !
Arveer Singh I've heard it before ! :D But it's not the case, have a nice day brother !
Bucky knows Java, Visual Basic, C++, 3Ds Max, Chemistry and more! This guy's a legend! Thumps up for this guy!
i pretty sure 1 < 1000 will only loop 999 times xd
+Joshua Caponong no it will loop 1000 times, but the variable will count from 0-999, instead of 1-1000
+Jackson Payne lol it won't start in 0 since roll is declared to 1 and is set to increment less than 1000.
Oh wow I guess he missed that
+Joshua Caponong what would've happened if we'd declared roll=45 or whatever?
It will start at 45
@Quentello \t means tab, the backslash is an escape character that tells java that the next character isnt part of what is inside the " " so it doesnt print the t, and also does something about it, there is a list of what each character does, for example a
would skip a line, you can try it out.
30 videos is done!!..57 videos more need to view..
Good Job Bucky
where r u now?
where r u now?@@nayaaccount5575
You are literally the reason I am passing my programming fundementals course thise semester. Thank you so much
omg you are so awesome :D keep up the good work!
Wow, this took me so long to understand. Basically the random number generator generates a number between 0 to 5. We counter this by adding +1. Whatever number is then generated is what the index is. The array then looks at the index, finds the number, and then adds 1 to it, via the ++freq part. This is how it stores the numbers in the array
wouldn't your roll count in your "for" loop actually roll 999 times, and not 1000?
ya u r right! if u try to add frequency of all the faces it is equal to 999
you're right, I didn't realize he started the loop from 1 and has "
hi guys i am new in youtube and i am making java tutiorals i would appreciate if you subed thanks
I got it!!!! It took me 5 rewatches until I gave up on his explaining. I just paused the video at the end and broke it down by myself ^_^
Hi guys.Pretty understandable this tutorial ,however, could anyone explain me what is the difference of putting ''freq[1+rand.nextInt(6)]++'' instead of ''++freq[1+rand.nextInt(6)]'' ?
Found it on StackOverflow : stackoverflow.com/questions/4752761/the-difference-between-n-vs-n-in-java
pre-incrementing changes the value right away, so if a=5, print(++a) will give 6, post-incrementing changes the value at the second time you use it, so print(a++) will give 5, but the second time if you print(a), it will give 6.
To this program, a++ and ++a both work
Yeah, ++n will immediately return the new value, while n++ will increment the variable but will return the old one
Both are quite similar but using ++a is a safer option since it increments the value immediately as soon as it encounters a face of the die.... but a++ increments the value of the slot after the statement gets executed once, in this case since it's a thousand rolls it's so difficult to spot, but better to use ++a in this scenario.
It works as well as you don't need to put "1"+rand.nextInt(6). You can put " rand.nextInt(7) "
Sorry but that’s wrong, 1+rand makes it so you don’t waste a roll on a 0. Dice starts at 1 and ends at 6.
0:12 - 0:17 LOL. Sorry thats just funny. Random numbergenerater-rher-re-roll the dice.
For anyone who is confused with how the for loop works here's an explanation.
roll starts at 1 (int roll = 1;) this declares an int called roll and initialises it to hold a value of 1. This only happens once in this program.
The program now looks at the second part of the for loop (roll < 1000) as roll holds a value of 1 and 1 < 1000 is true the program now enters the for loop.
rand is the name of his Random object and he uses one of Random's methods called nextInt(int) which will return
Lol that Babe reference though
Where? I didn't notice
Took a minute to wrap my head around that one too. What I think it says is that ++freq[3] would have been ++freq[1+2]. This is to avoid placing any values in the 0 element (array position 0). So yes this would place it into array position 2. Face on the other hand starts at 1 so therefore it would be placed in face 3. Remember, array starts at 0, Face starts at 1. Hope this helps.
instead of simply everytime typing System.out.println(""); just type syso and then control +Enter !
sucha smart mouth you are! you think he doesn't know that?! he is tryina be clear for everyone!
yakiyokititti see... I just made a suggestion & in this way people who see buckys tutorials can also come across this shortcut...
#keepCalmBro
yakiyokititti I didn't know! Thanks for the share Aadarsh Sree
Aadarsh Sree Aren't you supposed to press space instead of enter? on windows anyways?
Jamie Lange Lol I tried ctrl+enter, didn't work. Then saw your comment about ctrl+space, worked. Thanks!
Now this one was quite hard to grasp at first, but I am happy I finally understood what was going on :)
I think it should be
+Sam yep. can certainly be verified by totaling the frequencies.
+Sam You could do either that or just make "int roll=0"
After looking at this a few hours ago then coming back to the code I copied in my IDE, I finally have a better understanding as to why this works. I was so mad at myself because it seemed so easy but I couldn't grasp why the code worked. For me, the main thing that was throwing me off was the ++freq[1+rand.nextInt(6)]. What made it click for me was putting freq[1+rand.nextInt(6)] += 1; in my IDE just to see it work that way. I put comments by a few lines of code to explain it to myself in my IDE. :D
Oh, also roll
this program is more clever than many beginners think!
Hey guys who dont understand this one: ++freq[1+rand.nextInt(6)];
You just adding one into random array 1-6 each round (for cyklus) you add one into just one of this 6 arrays. To understand better copy this code into your main:
Random rand = new Random();
int Sebarray[] = new int[5] ;
int counter=1;
for (int roll=1;roll
Cheers! And nice choice of array name ;)
0:15 the legend did it again
DON'T think you're stupid for not getting the code right away, Bucky is to blame for not expanding the array indexes like that, :
freq[0] = 0
freq[1] = 0
freq[2] = 0
freq[3] = 0
freq[4] = 0
freq[5] = 0
freq[6] = 0
Then ++freq would have made sense right away. See, anytime you land a number, you just tick one at the zeroes on the right side.
yeah right. this makes sense
hi guys i am new in youtube and i am making java tutiorals i would appreciate if you subed thanks
Watched like 7 times, stared at it, listened to it, then I finally got it :D :D
+GiraffeKey da Fantabulous I came to see if someone has cleared it out in the comments and yeah there was one :D
Actually, you rolled the dice out 999 times not 1000.
Just adding 1+1000 will equal 1000 rolls.
JOSUE VELA or just
***** yea
JOSUE VELA woooooooooot
*****
or just
If anyone is confused i would suggest going and watching not just one of these tutorials (one "series") but watching one series until it gets too confusing because you don't really know/understand what was taught in the previous tutorials so instead of going back and watching them again the old ones which you probably wouldn't pay attention to anyway since you have already seen it but instead go to a different language playlist and solidify your understanding of the basics / previous material.
I am learning this for my new job and the explanations that you use are brilliant! I didn't get the ++freq[1+rand.nextInt(6)] in the beginning but now... keep up the awesome vids!
To those who don't understand how the array is adding 1 to the value:
freq[] = 1+rand.nextInt(6);
can be rewritten as
int test = 1+rand.nextInt(6);
freq[test] = 1 + freq[test];
I make a new integer equal to 1 + a random number between 0-5
I set the array position for that random number equal to 1 more than it was before.
So for example:
If the random number generator gives us a two, we add 1 to it, getting 3.
We make the number assigned to three in the array equal to 1 + what ever it was before.
Hope that helps.
So, just for the fun of it I wanted to make a program that would also show me the individual rolls and not just the frequency of the results, and gave me a way to choose how many dice I wanted to roll. I struggled a bit but finally:
import java.util.Random;
import java.util.Scanner;
class test3 {
public static void main(String[] args){
Random rand= new Random();
int num;
int freq[]=new int[7];
Scanner in = new Scanner(System.in);
int dice;
System.out.println("How many dice? ");
dice = in.nextInt();
in.close();
System.out.println("
Roll\tFace");
for(int roll=1;roll
Bucky is WAAAAAY better then my java manual which I spent SO much money on!
@kotb90 the part with "1+rand.nextInt (6)"will give you a random number between 1 and 6 (see tutorial 29)
so, for example the random will give you ++freq[4].
remember that if you have variable "bucky" and you want to add 1 to the current value, you could do ++bucky to do so.
so when we do ++freq[4] we take the current value of freq[4], and add 1 to it. that way we count how many time the random finds "4" as an answer.
make your program roll the dice 1,000,000 (million time) have to wait about 10 to 20 seconds for it to catch it was very fun finding the output all roughly the same value. Great tutorials thanks a lot for all your hard work!
30 down and i don't know how many to go then i'll be applying for work, thanks bucky!
for those who didn't understand the ++freq[index] , I have done bucky's code in a more understandable way that if you compare them you will definitely get the point. Hopefully, it would be helpful. you just need to create a test class test.java in copy paste the code blow.
import java.util.Random;
import java.util.Scanner;
public class test {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Random dice = new Random();
int roll, rndNumber;
int freq[] = new int[7];
int freq2[] = new int[7];
System.out.println("how many random number: ");
roll = input.nextInt();
for(int i = 0; i < roll; i++){
rndNumber = 1+dice.nextInt(6);
if(rndNumber == 1){
freq[1] += 1;
}else if(rndNumber == 2){
freq[2] += 1;
}else if(rndNumber == 3){
freq[3] += 1;
}else if(rndNumber == 4){
freq[4] += 1;
}else if(rndNumber == 5){
freq[5] += 1;
}else if(rndNumber == 6){
freq[6] += 1;
}
++freq2[rndNumber];
}
for(int face = 1; face < freq.length; face++){
System.out.println(face + "\t" + freq[face]);
}
System.out.println("-----------------");
for(int face = 1; face < freq.length; face++){
System.out.println(face + "\t" + freq2[face]);
}
}
}
@CODtheater I love Eclipse! After coming from the IDLE interpreter in Python and Xcode 3.x.x in Objective-C, this feels so much better!
Helped me as well. I was confused there and your explanation helped. Thanks!
Alright, thanks for your quick reply! I'll try that out. Although I still am wondering why it doesn't work properly. But good to know that (in theory) I wasn't (all that) wrong :)
Where you place the ++ just decides whether its a pre-increment or a post-increment. What that means is that pre-incrementing adds 1 BEFORE the rest of that line is ran, and post-incrementing adds 1 AFTER the code on that line was ran. So, in this circumstance, the effect of that change in the code will not affect the outcome.
Nice set of tutorials. Thanks for taking the time to do them.
Genius programming :D took me a few seconds to get my head around what
++freq[1+rand.nextInt(6)];
did.
These tutorials are amazing! I love them! Learning java is so much fun with bucky.
ok im just basing this off my understanding of data structures in general but I ran this code with 1 rather than 100 and such and it supports my interpretation: the freq[] is just calling a random element of the list (there being 6 elements, or "slots" to chose from). the ++ just adds one to that random element, which is a value of 1-6 for the 6 available slots- the random number generator is just giving a random index- if you wanted a 12 sided die youd type freq[1 + rand.nextInt(12)]
took me a bit, but i fully understand the programming behind this. i makes sense to me :D
RNG (random number generator) in Java is alot easier than C++. Atleast there is no seeding etc. As always thank you Bucky for that awesome video.
-Any random number was generated by rand which saved in freq[] array.
-face is just temporary variable. You can change it as whatever kind different you want. Its function is show index of array freq[].
Java is nearly as fast as C++ now. It used to be a lot slower than C++ but now it has caught right up in speed.
Little tip for anyone out there who wants to become a programmer. Don't become a poster boy for a certain language. You may think that one language is better than the rest which will mainly be due to your knowledge and enjoyment of that language but every programming language has its uses.
this tutorial caught me for like 3 days
Thanks heaps for this, I was really confused about how this works and your post really helped me out.
@TheFredemand it should work, make sure your code is as follows for(int face=0;face
So Do I... Actualy,I have but I make sure to return to these videos.
Bro, your videos are top notch lol. "Freq.....not that type of freq"
This outputs the same result and may be less confusing to understand. To get the values in the index to 1-6 you simply add 1 to the face in the System.out.println code. Also to get 1000 rolls, you can either change the roll
remember freq is an array and it needs a number to choose the index and [face] is an int which will pick whichever index
++freq[random number between 1 & 6 inclusive] adds 1 to the value at that index.
Remember we access arrays through index numbers. freq[0] would allow us access to the first value in the array. Because they aren't initialized to begin with they are all 0 right now. If our random number generator rolls a 6 (freq[6]) it will add 1 to that, so the value at the index freq[6] = 1. If we get six again, freq[6] = 2.
I will keep generating random numbers and adding 1 to an element in the array (index)
Okay guys, to learn this try to really break it down. What I mean by this is instead of looking at
'++freq[1+rand.nextInt(6)];'
Try to first identify what the random generator does:
'++freq[rand.nextInt(6)];'
Then add the +1:
'++freq[1+rand.nextInt(6)];'
That's how I learnt
Instead of wasting an array slot for face zero, we can just output face+1, and drop the 1+ in the random for loop. The full code is below, from another user.
A general advice for everyone, try to take sleeping rest every 10 episodes and keep thinking in your head of all the codes you learned. Now I can do almost all the previous 29 tutorials from my mind. FFS I dream about java now.. WTF! Lmao
That is such a helpful class. That's probably how twitters trending tweets work.
wow this actually helped me a lot its clear to me now thanks
Great tutorial! One minor error though, you "rolled the dice" 999, not 1000 times. Changing your first for loop to for(int roll = 1; roll
I liked all your videos so far.
Smart presentation. Thank you very much
1000thanks to you Bucky!!! You are the best of bests and also you saved my ass with java programing :D
A really minor exercise for who ever wants.
tell the program to say , what's the total times you've rolled the dice.
It taught me few new concepts I didn't know , important onces.
The first for loop was explained by Toadfoster (Look at top comments)
Remember that in the for loop, it goes for(starting point; end point; increment)
So the starting was 1, and ending LESS than 7 (Length of Array) but NOT EQUAL to 7, so we get exactly 6 'tables' Then we print out the 'face' number which goes under 'Roll' which starts at 1, ends at 6. The freq[face] will print all freq[1] freq[2]...freq[6] values, that were assigned in the first for loop. Ask if you need any further explanation
This Was The Best ... Keep Up The Good Work
you are aware this video is 7 years old right?
bucky still makes videos, tho
Kenneth Fleming not java tutorials though
that doesn't negate the fact of his videos being still good work
Constantine, I think Bucky uses ++freq[] instead of freq[]++ because he wants to first calculate the new array index number and then increment the value - as Tamerlan said. Using Tamerlan's example on the first loop with freq[]++, the result would be to increment freq[0] by 1 and then calculate the new index value of 3.
If you guys don't want to use eclipse you can use BlueJ its pretty good for beginners because it color codes the classes and methods, but I would recommend eclipse.
a much easier way to do the random number, where you don't have to import anything is just type: (int)(Math.random()*6+1);
This does the same thing but much quicker. The "int" rounds off the number to be a whole number, and the +1 again is because Math.random is a random number bigger then 0 and less than 1.
int freq[] has 7 elements and the value of each element is set zero by default. e.g. ++freq[1+rand.nextInt(6)] => ++freq[3] => freq[3] = 0+1 or freq[3] = zero initially and it is being incremented by 1. In the output there is a frequency of element 3 or freq[3]
Oh btw this prgram is GREAT thanks bucky:) but it will alwaaaayys be 1 digit off unless you put roll
so from what've understood, [ 1+rand.nextInt(6)] generates the array indexes from 1 to 6. the ++ freq adds the occurances of similar indexes. In the beginning all the freq[indexNr] are equalto zero since the rolls havent happened yet roll 1--> index=2,-->freq[2] =1 (++freq[2]--> freq[2] =freq[2]+1 or occuranceOf2= 0+1 becomes 1occurances of freq[2]) roll 2 --> index=4 -->freq[4]=1 (++freq[4]--> freq[4] =freq[4]+1 or occuranceOf4= 0+1 becomes 1 occurances of freq[4] ) roll 3-->-index= 2-->freq[2] =2 (++freq[2]--> freq[2] =freq[2]+1 or occuranceOf2= 1+1 becomes 2 occurances of freq[2])
roll4-->index =5 --> freq[5]=1 (++freq[5]--> freq[5] =freq[5]+1 or occuranceOf5= 0+1 becomes 1occurances of freq[5]) roll5 -->index=2--> freq[2]=3 (++freq[2]--> freq[2] =freq[2]+1 or occuranceOf2= 2+1 becomes 3 occurances of freq[2]) roll 6-->index =4--> freq[4]=2 (++freq[4]--> freq[4] =freq[4]+1 or occuranceOf4= 1+1 becomes 2 occurances of freq[4]) etc After this loop comes the print statement where the number of the dice is printed next to the total sum of each of the index occurancesnof the array, so basically the faces of the dice are the index numbers of the array. Hopefully this helps a bit
Just ignore the the lines through the words, that just happened when i posted this comment
it's because of the ---. it causes words to be ---crossed--- out and * causes words to be made *bold*
yes, that is correct, although that is a very interesting way of explaining it lol, but you fully understand the logic behind this program, congrats
@Soulscient Nope. 1+random adds one on every roll. 1+freq adds one to the value of the array on the position freq[1+rand.nextInt(6)], that is the number of roll in which you got that value.
I had neve considered doing this before, really cool tutorial:)
Hey guys, if you just watch the these videos from the first tutorial, you'd know what this means. He explained this in tutorial 8 or 9.
hopefully i see you next tutorial and yes hopefully he saw us every tutorial
Beautiful code. Thanks!
The ++freq bit means add 1 to the value that is stored in freq[1+rand.nextInt(6)] so if 1 + rand.nextInt(6) comes out at 3. The number of times a 3 would have been rolled will need to increase by 1 which is what ++ does.
Hope someone found that helpful in some way.