diff --git a/configure.ac b/configure.ac
index 0f70178245891d6b33210ec156e72def6a65d052..894dad85530b29e1cc399617ad76a3ce575250b0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -178,7 +178,7 @@ AC_DEFINE([DSFMT_MEXP], [521], [DSFMT parameters.])
 # Checks for library functions.
 AC_FUNC_FORK
 AC_FUNC_MMAP
-AC_CHECK_FUNCS([gethostbyname gettimeofday clock_gettime memalign memmove memset munmap regcomp pselect select socket sqrt strcasecmp strchr strdup strerror strncasecmp strtol strtoul poll epoll_wait kqueue setgroups sendmmsg madvise pthread_setaffinity_np getline fgetln])
+AC_CHECK_FUNCS([gethostbyname gettimeofday clock_gettime posix_memalign memmove memset munmap regcomp pselect select socket sqrt strcasecmp strchr strdup strerror strncasecmp strtol strtoul poll epoll_wait kqueue setgroups sendmmsg madvise pthread_setaffinity_np getline fgetln])
 
 # Check for cpu_set_t/cpuset_t compatibility
 AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]], [[cpu_set_t set; CPU_ZERO(&set);]])],
diff --git a/src/common/prng.c b/src/common/prng.c
index fdd838d266aed50d55c59837dbb69f095289a6a5..773576dae03a253934f81618bc9ae47aa2c3a5f3 100644
--- a/src/common/prng.c
+++ b/src/common/prng.c
@@ -21,7 +21,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <config.h>
-#ifdef HAVE_MEMALIGN
+#ifdef HAVE_POSIX_MEMALIGN
 #include <stdlib.h>
 #endif
 
@@ -84,18 +84,18 @@ double tls_rand()
 
 		/* Initialize PRNG state. */
 #ifdef HAVE_MEMALIGN
-		s = memalign(16, sizeof(dsfmt_t));
+		if (posix_memalign((void **)&s, 16, sizeof(dsfmt_t)) != 0) {
+			fprintf(stderr, "error: PRNG: not enough memory\n");
+			return .0;
+		}
 #else
-		s = malloc(sizeof(dsfmt_t));
-#endif
-		if (s == NULL) {
+		if ((s = malloc(sizeof(dsfmt_t))) == NULL) {
 			fprintf(stderr, "error: PRNG: not enough memory\n");
 			return .0;
-		} else {
-			dsfmt_init_gen_rand(s, seed);
-			(void)pthread_setspecific(tls_prng_key, s);
 		}
-		
+#endif
+		dsfmt_init_gen_rand(s, seed);
+		(void)pthread_setspecific(tls_prng_key, s);
 	}
 
 	return dsfmt_genrand_close_open(s);