xorshift init: 7731
A: 2064221648 1036493097 633233112 583013546 721278080 -1646392714 -829660162 478401127
E: 583013546 633233112 721278080 1036493097 2064221648 -1646392714 -829660162 478401127
Expected: -1646392714 -829660162 478401127 583013546 633233112 721278080 1036493097 2064221648
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define debug (0 || (sz < 50))
int cmpfunc(const void* a, const void* b)
{
int x = *(int*)a;
int y = *(int*)b;
return x-y;
}
unsigned int xorshift32(unsigned int x)
{
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
int
main()
{
#define sz 8
int* a = (int*)malloc(4*sz);
srand(time(NULL));
unsigned int init = rand(); printf("xorshift init: %lld\n",init);
int z=0;
for(int i = sz-1; i>=0;i+=0)
{
a[z] = xorshift32(init) % 0xD0000000U; init = a[z];
z++;
i--;
}
if(debug)
{
printf("A:\n");
for(int i = 0; i<sz;i++)
{
printf("%11d\n", a[i]);
}printf("\n");
}
qsort(a,sz,4,cmpfunc);
printf("E: \n");
if(debug)
{
for(int i = 0; i<sz;i++)
{
printf("%11d\n", a[i]);
}printf("\n");
}
}
win7_x64 | gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
No optimization or other kind of flags used.
"xorshift init: %lld\n"
(note%LLD
) ->"xorshift init: %11d\n"
– HolyBlackCattime(NULL)
). – Tomfor(int i = sz-1; i>=0;i+=0) { a[z] = ...; z++; i--; }
This looks suspicious, why not a simplefor(int z = 0; z < sz; z++) { a[z] = ...; }
? – Bob__