Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
What precisely is wrong with rand()?

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

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.

Contero
Mar 28, 2004

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.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

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 .

1337JiveTurkey
Feb 17, 2005

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.

mjau
Aug 8, 2008
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.

ToxicFrog
Apr 26, 2008


BonzoESC posted:

Most systems have a cryptographically secure PRNG:
http://msdn.microsoft.com/en-us/library/aa379942.aspx https://en.wikipedia.org/wiki//dev/random

That it's not the same function in all of them is a poo poo excuse to use rand().

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.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

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.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

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.

shrughes
Oct 11, 2008

(call/cc call/cc)
The real reason rand() is a coding horror is because it has global state instead of thread-local state.

Chuu
Sep 11, 2004

Grimey Drawer

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.

Scaevolus
Apr 16, 2007

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.
I did the same. :v: If you still have any desire for AUTHENTIC FREECELL DEALS, I could dig up my code.

evensevenone
May 12, 2001
Glass is a solid.
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.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

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.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Scaevolus posted:

I did the same. :v: 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).

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
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());?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
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.

evensevenone
May 12, 2001
Glass is a solid.
If you make a variable called 'quit' in MATLAB, trying to close the shell will just evaluate the variable.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

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.

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.

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.

Chopper
Feb 13, 2006

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.

Opinion Haver
Apr 9, 2007

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.

zeekner
Jul 14, 2007

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.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

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 :v:

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.

1337JiveTurkey
Feb 17, 2005

Jabor posted:

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.

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.

mjau
Aug 8, 2008

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.
There's this thing called video games. You may have heard of it.

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

pigdog
Apr 23, 2004

by Smythe
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.

ToxicFrog
Apr 26, 2008


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.

PraxxisParadoX
Jan 24, 2004
bittah.com
Pillbug

Geekner posted:

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.

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.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


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. :frogc00l:

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Doc Hawkins posted:

Everyone who's anyone knows that NodeCoffee apps are the new hotness. :frogc00l:

When it's not a legacy app.

Mogomra
Nov 5, 2005

simply having a wonderful time
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.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Well, null means "explicitly nothing", while undefined is "haven't really thought about it".

Milotic
Mar 4, 2009

9CL apologist
Slippery Tilde

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?

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.

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.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

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?

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.

https://www.youtube.com/watch?v=bzkRVzciAZg

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

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.

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
http://golang.org/pkg/time/

quote:

These are predefined layouts for use in Time.Format. The standard time used in the layouts is:

Mon Jan 2 15:04:05 MST 2006

which is Unix time 1136243045. Since MST is GMT-0700, the standard time can be thought of as

01/02 03:04:05PM '06 -0700

To define your own format, write down what the standard time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples.

Within the format string, an underscore _ represents a space that may be replaced by a digit if the following number (a day) has two digits; for compatibility with fixed-width Unix time formats.

A decimal point followed by one or more zeros represents a fractional second, printed to the given number of decimal places. A decimal point followed by one or more nines represents a fractional second, printed to the given number of decimal places, with trailing zeros removed. When parsing (only), the input may contain a fractional second field immediately after the seconds field, even if the layout does not signify its presence. In that case a decimal point followed by a maximal series of digits is parsed as a fractional second.

:stonk:

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.

Wow, that is impressively awful format. It isn't spectacular like some of the PHP stuff, but it is still an everyday horror.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
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

Catalyst-proof
May 11, 2011

better waste some time with you
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?

Adbot
ADBOT LOVES YOU

pseudorandom name
May 6, 2007

The could've made format strings use "yyyy", "mm", "dd" etc. instead, which would be both easy to remember and easy to read.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply