bridge/class-master/depend/pari/src/basemath/random.c
/* assume N > 0, see random_Fl() for algorithm. Make sure that 32-bit and
 * 64-bit architectures produce the same integers (consuming random bits
 * by packets of 64) */
assume N > 0, see random_Fl() for algorithm. Make sure that 32-bit and
64-bit architectures produce the same integers (consuming random bits
by packets of 64)
assume N > 0 see random_Fl () for algorithm . Make sure that 32 - bit and 64 - bit architectures produce the same integers ( consuming random bits by packets of 64 )
GEN
randomi(GEN N)
{
  long lx = lgefint(N);
  GEN x, d;
  int shift;
  if (lx == 3) return utoi( random_Fl(N[2]) );
  shift = bfffo(*int_MSW(N));
  if (Z_ispow2(N) && ++shift == BITS_IN_LONG) { shift = 0; lx--; }
  x = cgetipos(lx);
  for (;;) {
    GEN y, MSW = int_MSW(x), STOP = MSW;
#ifdef LONG_IS_64BIT
    for (d = int_LSW(x); d != STOP; d = int_nextW(d)) *d = rand64();
    *d = rand64() >> shift;
#else
    if (!odd(lx)) STOP = int_precW(STOP);
    for (d = int_LSW(x); d != STOP; d = int_nextW(d))
    {
      ulong a, b; _64to32(rand64(), &a,&b);
      *d = b; d = int_nextW(d);
      *d = a;
    }
    {
      ulong a, b; _64to32(rand64() >> shift, &a,&b);
      if (d == MSW) 
        *d = a;
      else
      { 
        *d = b; d = int_nextW(d);
        *d = a;
      }
    }
#endif
    y = int_normalize(x, 0);
    if (abscmpii(y, N) < 0) return y;
  }
}
GEN randomi ( GEN N ) { long lx = lgefint ( N ) ; GEN x , d ; int shift ; if ( lx == 3 ) return utoi ( random_Fl ( N [ 2 ] ) ) ; shift = bfffo ( * int_MSW ( N ) ) ; if ( Z_ispow2 ( N ) && ++ shift == BITS_IN_LONG ) { shift = 0 ; lx -- ; } x = cgetipos ( lx ) ; for ( ; ; ) { GEN y , MSW = int_MSW ( x ) , STOP = MSW ; #ifdef LONG_IS_64BIT for ( d = int_LSW ( x ) ; d != STOP ; d = int_nextW ( d ) ) * d = rand64 ( ) ; * d = rand64 ( ) >> shift ; #else if ( ! odd ( lx ) ) STOP = int_precW ( STOP ) ; for ( d = int_LSW ( x ) ; d != STOP ; d = int_nextW ( d ) ) { ulong a , b ; _64to32 ( rand64 ( ) , & a , & b ) ; * d = b ; d = int_nextW ( d ) ; * d = a ; } { ulong a , b ; _64to32 ( rand64 ( ) >> shift , & a , & b ) ; if ( d == MSW ) * d = a ; else { * d = b ; d = int_nextW ( d ) ; * d = a ; } } #endif y = int_normalize ( x , 0 ) ; if ( abscmpii ( y , N ) < 0 ) return y ; } }
assume N > 0, see random_Fl() for algorithm.
assume N > 0 see random_Fl () for algorithm .
/* if N a power of 2, increment shift */ /* STOP points to where MSW would in 64-bit */ /* 32 bits needed */ /* 64 bits needed */