|
Just print it, so yeah you're right. I'm stupid and didn't realise that. Thanks
|
# ? Jan 9, 2009 19:34 |
|
|
# ? Jun 7, 2024 08:28 |
|
You shouldn't seed the random number generator every turn, only once at the beginning of your program.
|
# ? Jan 9, 2009 21:48 |
PT6A posted:You shouldn't seed the random number generator every turn, only once at the beginning of your program. Seeding more makes it more random!
|
|
# ? Jan 10, 2009 00:55 |
|
hexadecimal posted:
Are you sure?
|
# ? Jan 10, 2009 03:19 |
|
floWenoL posted:Are you sure?
|
# ? Jan 10, 2009 03:22 |
|
floWenoL posted:Are you sure? Oh... lol you got me there. %2 would do what i meant to do. I was just thinking to generate number 0 or 1, and forgot that %1 is meaningless. Sorry guys. hexadecimal fucked around with this message at 03:28 on Jan 10, 2009 |
# ? Jan 10, 2009 03:24 |
|
hexadecimal posted:Oh... lol you got me there. %2 would do what i meant to do.
|
# ? Jan 10, 2009 03:29 |
|
Dijkstracula posted:I'm curious, why did you suggest this way rather than permuting an ordered list of the pairs? Can you show me how to do this?
|
# ? Jan 10, 2009 03:30 |
|
hexadecimal posted:Can you show me how to do this? code:
Dijkstracula fucked around with this message at 03:40 on Jan 10, 2009 |
# ? Jan 10, 2009 03:35 |
|
hexadecimal posted:Can you show me how to do this? Maybe you'll learn it in grad school!
|
# ? Jan 10, 2009 03:40 |
|
Dijkstracula posted:
Ugh, this still isn't a properly shuffled array.
|
# ? Jan 10, 2009 03:41 |
|
Dijkstracula posted:
The reason I didn't do this is because to get index in linked list is linear time, and to swap in array is more costly than linked list. So I thought the way I did it would be fastest, even if it doesn't really generate true random permutation. Perhaps if one had a custom linked list implementation you could store references to all 9 nodes in array, then do your method, and swap pointers in the node references. This would probably be the fastest way?
|
# ? Jan 10, 2009 03:42 |
|
Ugg boots posted:Ugh, this still isn't a properly shuffled array. Can we just elide this discussion by pointing him here: http://en.wikipedia.org/wiki/Knuth_shuffle ?
|
# ? Jan 10, 2009 03:44 |
|
Ugg boots posted:Ugh, this still isn't a properly shuffled array. vvv Kluth Shuffle is linear, so I don't think that's it. Dijkstracula fucked around with this message at 03:49 on Jan 10, 2009 |
# ? Jan 10, 2009 03:45 |
|
Dijkstracula posted:What's missing? Is n swaps too few for an array of size n? If you're not avoiding swaping the same two numbers twice it is. Edit: This is kind of out of my rear end, but I seem to remember that if you swap without avoiding duplication you need n log n swaps. Edit2: Upon further thought I'm pretty sure that's right because it would seem to follow from Fisher-Yates + Coupon Collector Smackbilly fucked around with this message at 03:49 on Jan 10, 2009 |
# ? Jan 10, 2009 03:46 |
|
^^^^ wrong (Edit: actually nevermind, i misinterpreted what you mean by "swap without avoiding duplication")Dijkstracula posted:What's missing? Is n swaps too few for an array of size n? code:
floWenoL fucked around with this message at 04:03 on Jan 10, 2009 |
# ? Jan 10, 2009 03:47 |
|
hexadecimal posted:to swap in array is more costly than linked list. Not true unless the elements are large. quote:Perhaps if one had a custom linked list implementation you could store references to all 9 nodes in array, then do your method, and swap pointers in the node references. This would probably be the fastest way? Nope. *puts in hexadecimal.txt* floWenoL fucked around with this message at 03:54 on Jan 10, 2009 |
# ? Jan 10, 2009 03:50 |
|
floWenoL posted:An easy way to see that your method is wrong is that n^n (number of possibilities in your method) isn't evenly divisible by n! (number of shuffles of an array). Victor fucked around with this message at 04:46 on Jan 10, 2009 |
# ? Jan 10, 2009 03:52 |
|
Victor posted:Genius! I suppose this is a standard way of reasoning if you have formal CS or maths training, but i don't. I had seen this idea before, but I think you just locked it in my slow brain. edit: yep, I'm still embarassed. Dijkstracula fucked around with this message at 03:56 on Jan 10, 2009 |
# ? Jan 10, 2009 03:53 |
|
Fixed all the tic tac toe issues and that's working nicely now. I'm on another small program and have come across an annoying bug:code:
|
# ? Jan 11, 2009 20:37 |
|
I'm not sure about an infinite loop, but those branching if-statements are definitely screwed. Example: code:
What you need is (n == 1). That use of the == checks whether or not the two sides are equal, rather than assigning one to the other. Secondly, your for loop will only do anything the first time through. Every if-statement after 0 is contained inside the 0 if-statement - so it'll work the first time, when n == 0, but that's it. I think you need something like: code:
code:
code:
|
# ? Jan 11, 2009 20:48 |
|
Ahhh thank you. I always forget to use two = rather than just one. I guess I'm used to using Visual Basic, rather than C++. It's an annoying jump, due to the syntax. The card game isn't much, it just scores a hand of Blackjack
|
# ? Jan 11, 2009 20:54 |
|
Staggy posted:Alternatively you could use a Switch-Case. Yes, let's teach people the for-case paradigm. You'd be better off writing a function which deals with printing the statement, and fetching the input, then calling it 5 times, either via a loop and an array of ints, or just by calling it 5 times explicitly. EDIT: or, since it's a relatively small amount of logic, just shoving it inline with the array digibawb fucked around with this message at 20:57 on Jan 11, 2009 |
# ? Jan 11, 2009 20:54 |
|
digibawb posted:Yes, let's teach people the for-case paradigm. Tell me more? Our programming lecturer guy seems to encourage switch-cases.
|
# ? Jan 11, 2009 20:58 |
|
honeymustard posted:Tell me more? Our programming lecturer guy seems to encourage switch-cases. There's nothing wrong with switch-case, it's awesome. Doing it inside a loop based on the iterator is just silly however (in general anyway). With a small case like this it might not look all that crazy, but once you go down the road, you get to places like http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx
|
# ? Jan 11, 2009 21:01 |
|
Hmm, I think I understand that. What I originally wanted was one line within the loop, like: cin >> cardvalue[n] So each time it would increment the variable name itself, but I couldn't figure out how to use that, or if it is even possible.
|
# ? Jan 11, 2009 21:09 |
|
honeymustard posted:Hmm, I think I understand that. What I originally wanted was one line within the loop, like:
|
# ? Jan 11, 2009 21:11 |
|
honeymustard posted:Tell me more? Our programming lecturer guy seems to encourage switch-cases. Yeah, for something like that, you definitely want to use a switch, because with a series of if statements, it needlessly runs every single statement; whereas with a switch, it jumps right into the case derived from the argument, and only runs that case. Just keep in mind that you can only use either ints or chars as an argument in a switch.
|
# ? Jan 11, 2009 21:30 |
|
Anunnaki posted:Yeah, for something like that, you definitely want to use a switch, because with a series of if statements, it needlessly runs every single statement; whereas with a switch, it jumps right into the case derived from the argument, and only runs that case. Just keep in mind that you can only use either ints or chars as an argument in a switch. "Premature optimization is the root of all evil." While switch statements can usually be implemented as jump tables (which are faster), 99% of the time, you won't notice the speed difference anyway. Always, always write code in the clearest way possible first, then optimize later if you need to. That said, I think switch statements are usually more readable so I use them anyway.
|
# ? Jan 11, 2009 21:34 |
|
Avenging Dentist posted:"Premature optimization is the root of all evil." While switch statements can usually be implemented as jump tables (which are faster), 99% of the time, you won't notice the speed difference anyway. Always, always write code in the clearest way possible first, then optimize later if you need to. I agree with you, but why are switch statements less clear then if statements? I mean if you follow a relatively sane coding style then switch statement should be about as readable as an alternative if one. EDIT: Didn't see your edit.
|
# ? Jan 11, 2009 21:36 |
|
honeymustard posted:Ahhh thank you. I always forget to use two = rather than just one. I guess I'm used to using Visual Basic, rather than C++. It's an annoying jump, due to the syntax.
|
# ? Jan 11, 2009 21:58 |
|
Thanks, got that part sorted. One other thing I was wondering, when using a case-switch statement, is it possible to have more than one case on the same line? I thought it'd look like: case ('2' || '3'): but that doesn't seem to work.
|
# ? Jan 11, 2009 22:24 |
|
honeymustard posted:Thanks, got that part sorted. One other thing I was wondering, when using a case-switch statement, is it possible to have more than one case on the same line? I thought it'd look like: code:
|
# ? Jan 11, 2009 22:25 |
|
Wonderful, works great, cheers
|
# ? Jan 11, 2009 22:26 |
|
sklnd posted:If you have trouble with == versus = (and I've seen old grizzled coders check in bugs related to that), place the constant portion of the comparison on the left. It might not be exactly how you would describe the logic if you were reading it, but it makes the compiler catch a dumb error. That is dumb and horrible. If you have trouble with == versus = and your compiler doesn't tell you you need to turn warnings on (and are probably missing a load of other stuff, too). Edit: You really shouldn't be using a for loop and the switch statement.
|
# ? Jan 12, 2009 01:33 |
|
floWenoL posted:That is dumb and horrible. If you have trouble with == versus = and your compiler doesn't tell you you need to turn warnings on (and are probably missing a load of other stuff, too). Example: http://codepad.org/gIhxXXaN
|
# ? Jan 12, 2009 01:35 |
|
sklnd posted:If you have trouble with == versus = (and I've seen old grizzled coders check in bugs related to that), place the constant portion of the comparison on the left. It might not be exactly how you would describe the logic if you were reading it, but it makes the compiler catch a dumb error. The entire rationale just doesn't make sense. It makes more sense to be aware when you are using the wrong operator rather than to write your code in a way that may be unintuitive to yourself or a reader for little [or in my opinion, no] gain. That Turkey Story fucked around with this message at 02:27 on Jan 12, 2009 |
# ? Jan 12, 2009 02:25 |
|
That Turkey Story posted:I bring this up every time someone mentions it, but I really don't understand how this is supposed to help -- if your problem is that you are forgetting to use == instead of =, I don't see how putting constants on the left is any easier to remember. In other words, if you are writing your comparison and you have the sense to say to yourself "I'd better put the constant on the left so if I forget to use the == operator it'll produce a compile error," why not at that point in time just make sure to use == instead of =, after all you just remembered to put the constant on left, why not just be sure to use == instead. Also what about times where neither operand is constant, do you find yourself using the wrong operator there? Probably not. It doesn't help when both values are variables, but instances where doing that is habit would catch bleary-eyed mistakes of just forgetting the second =, or just when a VB person comes along and played in a C-like language. As pointed out in #cobol, -Wall is probably a better option.
|
# ? Jan 12, 2009 03:09 |
|
Hmm, I was bored and thought I'd try to figure out how to make a program that computes every single permutation of letters in the alphabet. After I got it working initially, I realized my math was horribly off. I figured if you moved "A" to the last position, then "B" to the second to last, "C" to the third to last, etc., the math would basically come out to be "25 + 24 + 23 + .. + 1", but realized it's actually multiplied by instead of added by, which makes it "26!", which is freaking huge. Is it feasible to come up with an algorithm that generates every possible permutation? Because we're talking 4.03 x 10^26 possibilities.
|
# ? Jan 12, 2009 10:07 |
|
|
# ? Jun 7, 2024 08:28 |
|
Anunnaki posted:Is it feasible to come up with an algorithm that generates every possible permutation? Because we're talking 4.03 x 10^26 possibilities. That's at least about as hard as incrementing an 89 bit counter through its range, so no, it's not computationally tractable.
|
# ? Jan 12, 2009 10:16 |