|
What precisely is wrong with rand()?
|
# ? Apr 10, 2012 21:54 |
|
|
# ? May 14, 2024 21:22 |
|
Sinestro posted:What precisely is wrong with rand()? Windows is huge on backwards-compatibility, and so on windows rand() returns the exact same sequence of numbers it did back in Windows 95.
|
# ? Apr 10, 2012 22:22 |
|
Jabor posted:Windows is huge on backwards-compatibility, and so on windows rand() returns the exact same sequence of numbers it did back in Windows 95. Ew, windows 95? Those numbers are stale as gently caress. Personally, I get a fresh PRNG every other week.
|
# ? Apr 10, 2012 22:28 |
|
Sinestro posted:What precisely is wrong with rand()? From time immemorial, the implementation of rand(3) in the libc that ships with msvc ("windows rand()") has had very poor entropy in the lower-order bits. This means there are noticeable patterns that lead to predictability. The fact that the lower-order bits are less random than the higher-order bits is even more problematic when you consider common usages like rand() % 20 .
|
# ? Apr 10, 2012 22:35 |
|
There's lots of ways people get random numbers wrong and many of them aren't related to the algorithm used by the PRNG. It's still possible to get skewed probabilities even with a generator like Mersenne Twister. For example if a PRNG or even a genuine entropy source produced an output from 0-16, if you restricted the output modulo 15, the number 0 would be twice as likely to show up as all the other numbers.
|
# ? Apr 10, 2012 22:42 |
|
Having the same RNG as in '95 wouldn't be a problem if it was any good, but it isn't. First of all RAND_MAX is defined as 32767, which isn't a lot, and lower bits are less random than upper bits. Even if you try to account for this, there's also obvious patterns in the generated sequences, specially if you visualize them. The worst example of this I've seen was one where rand() was used for temporal noise, among some other things -- on Windows, there was fixed clear vertical lines in the output.
|
# ? Apr 10, 2012 22:42 |
|
BonzoESC posted:Most systems have a cryptographically secure PRNG: Yeah, I'm not saying "windows doesn't have a decent PRNG", I'm saying: - if you want to portable everywhere C is portable without a bunch of system-specific ifdefs, you need to use rand() or roll your own PRNG - if you are running on windows, the implementation of rand() is poo poo A lot of languages want to be portable everywhere C is (if for no other reason than that they're implemented in C) and don't want to implement their own PRNG (it takes effort and is yet another thing to get wrong). So they use rand(), which is the only thing they know will be available, and hey, it's not poo poo on most OSes, so that's good enough. Right? mjau posted:Well, if you want repeatability across different systems (e.g. for procedural generation), you'll want the RNG to come up with the same sequence every time it's given the same seed, but the same seed on two different RNGs will result in different sequences. ('course, if you care about repeatability like that you really should use a better defined RNG in the first place) Yeah, since there's no guarantee different systems or even different libc versions in the same system use the same algorithm for rand(), if you really need cross-system repeatability you need to use something with a fixed algorithm.
|
# ? Apr 10, 2012 22:45 |
|
dis astranagant posted:I'm still having trouble wrapping my head around how that's even supposed to work. The words all parse but I just can't make the logic happen. The result of the first ternary expression is fed in as the bool test of the next ternary expression, so imagine that the first test has parents around it and has its result coerced to Boolean to feed the next ternary on the right. Having trouble understanding why it outputs horse means you have functioning brain cells left.
|
# ? Apr 11, 2012 01:05 |
|
1337JiveTurkey posted:There's lots of ways people get random numbers wrong and many of them aren't related to the algorithm used by the PRNG. It's still possible to get skewed probabilities even with a generator like Mersenne Twister. For example if a PRNG or even a genuine entropy source produced an output from 0-16, if you restricted the output modulo 15, the number 0 would be twice as likely to show up as all the other numbers. One really dumbass thing I did one time was calling 'create new rand()' in the constructor for an object... The results were very much not random.
|
# ? Apr 11, 2012 01:10 |
|
The real reason rand() is a coding horror is because it has global state instead of thread-local state.
|
# ? Apr 11, 2012 03:44 |
|
Suspicious Dish posted:PHP For someone who wants to learn to write web apps (and has a lot of experience with traditional apps) where should you start? Most tutorials I see start by pointing you at a LAMP stack.
|
# ? Apr 11, 2012 04:04 |
|
Nippashish posted:PHP's rand() function seems to have some pretty obvious patterns. Woah, it's weird to see people link old stuff you've written. pokeyman posted:Yeah Windows rand() is loving worthless. Found that out when trying to reimplement the deals in FreeCell. It's got some ridiculously low period.
|
# ? Apr 11, 2012 04:05 |
|
If you need your random number generator to generate the same random numbers repeatedly cross-platform I think you really need to stop and consider what you are doing with your life.
|
# ? Apr 11, 2012 04:30 |
|
evensevenone posted:If you need your random number generator to generate the same random numbers repeatedly cross-platform I think you really need to stop and consider what you are doing with your life. Writing simulation software? I mean there's a definite use-case for a repeatable PRNG, it's just not the most common case that people use random numbers for.
|
# ? Apr 11, 2012 04:48 |
|
Scaevolus posted:I did the same. If you still have any desire for AUTHENTIC FREECELL DEALS, I could dig up my code. I ended up getting it working, found some random site with a text file that described the Windows rand() implementation and dicked around with dealing patterns until it clicked. Meantime I was laughing at all the FreeCell clones touting THE MISSING DEALS (because they called srand() with larger values than FreeCell ever did).
|
# ? Apr 11, 2012 05:05 |
|
How do FreeCell deals even work. Each thing has a "game number" -- is that the seed they provide to srand? And then choosing a random game is srand(TIME); srand(rand());?
|
# ? Apr 11, 2012 05:48 |
|
The game number is the random seed, yeah. Not sure how a random game is chosen, but you're probably about right. Here's that text file I mentioned. And I think there were two special game numbers, -1 and -2, one of which was winnable with a single move (if autoplay was turned on) and the other of which was highly impossible to win.
|
# ? Apr 11, 2012 05:56 |
|
If you make a variable called 'quit' in MATLAB, trying to close the shell will just evaluate the variable.
|
# ? Apr 11, 2012 06:25 |
|
pokeyman posted:The game number is the random seed, yeah. Not sure how a random game is chosen, but you're probably about right. Here's that text file I mentioned. To save everyone the effort: As of Windows 7, both -1 and -2 seem impossible. -3 and -4 are one-click to win. There is no -5.
|
# ? Apr 11, 2012 07:53 |
|
Chuu posted:For someone who wants to learn to write web apps (and has a lot of experience with traditional apps) where should you start? Most tutorials I see start by pointing you at a LAMP stack. Current fashionable languages/frameworks are Python/Django and Ruby/Rails. Personally I prefer Django.
|
# ? Apr 11, 2012 08:24 |
|
Chopper posted:Current fashionable languages/frameworks are Python/Django and Ruby/Rails. Personally I prefer Django. If you don't feel like using an entire framework I heard good things about Flask.
|
# ? Apr 11, 2012 08:56 |
|
Chopper posted:Current fashionable languages/frameworks are Python/Django and Ruby/Rails. Personally I prefer Django. And if you must use PHP, CodeIgniter is the only competent framework. Good documentation, reasonable design standards, no lovely comments by terrible coders in the documentation. It almost made PHP seem like a normal language, until I had to look up some core PHP functionality and came crashing back to earth.
|
# ? Apr 11, 2012 09:00 |
|
pokeyman posted:I ended up getting it working, found some random site with a text file that described the Windows rand() implementation and dicked around with dealing patterns until it clicked. I did the same thing But I only managed to find a VB6 implementation of rand(), and since VB6 uses different rules for integer overflows, they guy that had written it had added a whole bunch of code to adjust for that. So I had to rewrite it again to remove the adjustments.
|
# ? Apr 11, 2012 09:35 |
|
Jabor posted:Writing simulation software? If you need a repeatable PRNG, the code actually needed to implement one of your own is about as simple to make cross-platform as humanly possible.
|
# ? Apr 11, 2012 09:37 |
|
evensevenone posted:If you need your random number generator to generate the same random numbers repeatedly cross-platform I think you really need to stop and consider what you are doing with your life. Take everyone's favorite, for example: Minecraft. You can give it the same seed as someone else, and it'll generate the same world for you as they had! With all the trees intact, before they burned down! Amazing, isn't it? That's procedural generation in action. It's not limited to world creation, it can be used for anything. Tons of games use it, demos, it's even used for effects in movies. Look up perlin noise sometime. Repeatable randomness is also useful for simultaneous simulation in multiplayer games. In stead of having to wait for the server to simulate every detail of everyone's game, each client simulates its own game locally, only using the server to verify that someone didn't cheat. Reduces latency. If a game can record replays, or need to repeatedly replay the same state (a puzzly physics game, for example), it's essential that everything behaves the same as they did originally. If a ball bounced left and hit a bomb or something, it shouldn't randomly decide to bounce right on replays, messing up everything that happened after that. Repeatability is also very useful for debugging. etc vvv Yes, exactly mjau fucked around with this message at 10:59 on Apr 11, 2012 |
# ? Apr 11, 2012 10:10 |
|
You'd base your procedural generation on something else rather than buggy system-specific random, as in by definition unpredictable, number generation. Something you'd include as a part of the codebase for the game.
|
# ? Apr 11, 2012 10:28 |
|
Chuu posted:For someone who wants to learn to write web apps (and has a lot of experience with traditional apps) where should you start? Most tutorials I see start by pointing you at a LAMP stack. Pick a language you already know (or want to learn). Find a web framework or library for it. Rock on. For Python, I've used and liked Django and hear good things about Flask. For Ruby, Ruby on Rails seems to be what everyone is using. For Scala, Lift would be amazing if it were actually documented and Play is getting a lot of good press. This list is by no means comprehensive.
|
# ? Apr 11, 2012 15:26 |
|
Geekner posted:And if you must use PHP, CodeIgniter is the only competent framework. I wouldn't say *only*, Symfony2 is pretty drat good, albeit rather complicated. You can even use parts of the framework in Silex, which is more of a Flask/Sinatra kind of setup.
|
# ? Apr 11, 2012 18:30 |
|
Chopper posted:Current fashionable languages/frameworks are Python/Django and Ruby/Rails. Personally I prefer Django. Everyone who's anyone knows that NodeCoffee apps are the new hotness.
|
# ? Apr 11, 2012 18:48 |
|
Doc Hawkins posted:Everyone who's anyone knows that NodeCoffee apps are the new hotness. When it's not a legacy app.
|
# ? Apr 11, 2012 20:00 |
|
I'm actually really interested to hear what the issues with Node.js are. I keep hearing people say that it's the next PHP, but I don't hear any clear cut reasons why. Is it just that there's really no one way to skin a cat in javascript? Or is there even more craziness that I don't know about? e: Also null and undefined? Really? The more I think about it the more I think I'm answering my own question. I'd still like to hear everyone's input.
|
# ? Apr 11, 2012 20:47 |
|
Well, null means "explicitly nothing", while undefined is "haven't really thought about it".
|
# ? Apr 11, 2012 21:24 |
|
Mogomra posted:I'm actually really interested to hear what the issues with Node.js are. I keep hearing people say that it's the next PHP, but I don't hear any clear cut reasons why. Is it just that there's really no one way to skin a cat in javascript? Or is there even more craziness that I don't know about? I am not a node.js guru, but two core things are 1) callbacks and 2) event loops. The first tends to result in your code being "inside out" when chaining callbacks. The second has its place, but when abused results in some fiendishly difficult to debug systems. Also, as good as V8 is, it can only optimise so much given that it has to deal with a dynamic, weakly typed language. I'd also make the probably controversial statement that weakly typed languages aren't as amenable to the types of refactoring that happens on medium to larger scale projects. It's nice being able to make a change and know the compiler will scream at you to tidy up in all the places you'd completely forgotten about.
|
# ? Apr 11, 2012 21:57 |
|
Mogomra posted:I'm actually really interested to hear what the issues with Node.js are. I keep hearing people say that it's the next PHP, but I don't hear any clear cut reasons why. Is it just that there's really no one way to skin a cat in javascript? Or is there even more craziness that I don't know about? https://www.youtube.com/watch?v=bzkRVzciAZg
|
# ? Apr 11, 2012 22:07 |
|
Milotic posted:I am not a node.js guru, but two core things are 1) callbacks and 2) event loops. The first tends to result in your code being "inside out" when chaining callbacks. They're considering adding a promise/Deferred to the standard library instead of using continuation passing, which should help things a bit. Unfortunately, you can't emulate inlineCallbacks. Event loops aren't that big a deal when debugging, you just have to learn to deal with asynchronous programming. There are two major problems with node.js in my opinion: 1) that JavaScript has no facilities for asynchronous programming outside of continuation passing (if you use Mozilla JS, they have generator, which means that you can implement a simple trampoline). And 2) this may have changed considerably since 2010, but when I last tried to implement an application in node I felt like I was catching up with very rapid stdlib changes. I was handling multipart uploads, and twice in 2-3 months _ry decided that the multipart form API wasn't good enough, and rewrote it from scratch. I don't think _ry ever wrote anything based on node.js in that timeframe that was in production. This goes back to my belief that when you're not working in the trenches of your own platform every day, you cannot possibly know what the best addition to your own platform is.
|
# ? Apr 11, 2012 22:11 |
|
http://golang.org/pkg/time/quote:These are predefined layouts for use in Time.Format. The standard time used in the layouts is:
|
# ? Apr 12, 2012 00:18 |
|
Janin posted:http://golang.org/pkg/time/ Wow, that is impressively awful format. It isn't spectacular like some of the PHP stuff, but it is still an everyday horror.
|
# ? Apr 12, 2012 00:29 |
|
yeah, those format by example strings shook me the first time I saw them and I have to check the docs to write them but they do have the nice property that if I see t.Format("2006-01-02") in some code it's far more obvious what that will look like than the usual strftime format strings that I have to look up to read and write. v:(v
|
# ? Apr 12, 2012 00:34 |
|
Really? Of all the loving things to fall for NIH syndrome. Who doesn't know the strftime codes by heart? Is there some horrible flaw with strftime that I'm not aware of?
|
# ? Apr 12, 2012 00:49 |
|
|
# ? May 14, 2024 21:22 |
|
The could've made format strings use "yyyy", "mm", "dd" etc. instead, which would be both easy to remember and easy to read.
|
# ? Apr 12, 2012 01:14 |