#include <time.h>
`}
+in "C" `{
+ /* Is rand shortcut? */
+ static int nit_rand_seeded;
+ /* Current rand seed if seeded */
+ static unsigned int nit_rand_seed;
+
+ #define NIT_RAND_MAX 0x7fffffff
+
+ /* This algorithm is mentioned in the ISO C standard, here extended
+ for 32 bits. */
+ static int
+ nit_rand(void) {
+ unsigned int next = nit_rand_seed;
+ int result;
+
+ next *= 1103515245;
+ next += 12345;
+ result = (unsigned int) (next / 65536) % 2048;
+
+ next *= 1103515245;
+ next += 12345;
+ result <<= 10;
+ result ^= (unsigned int) (next / 65536) % 1024;
+
+ next *= 1103515245;
+ next += 12345;
+ result <<= 10;
+ result ^= (unsigned int) (next / 65536) % 1024;
+
+ nit_rand_seed = next;
+
+ return result;
+ }
+`}
+
redef class Int
# Returns a random `Int` in `[0 .. self[`.
fun rand: Int `{
+ if (nit_rand_seeded) return (long)(((double)self)*nit_rand()/(NIT_RAND_MAX+1.0));
return (long)(((double)self)*rand()/(RAND_MAX+1.0));
`}
# Returns the result of a binary AND operation on `self` and `i`
#
- # assert 0x10.bin_and(0x01) == 0
- fun bin_and(i: Int): Int `{ return self & i; `}
-
- # Alias of `bin_and`
- fun &(i: Int): Int do return bin_and(i)
+ # assert 0x10 & 0x01 == 0
+ fun &(i: Int): Int `{ return self & i; `}
# Returns the result of a binary OR operation on `self` and `i`
#
- # assert 0x10.bin_or(0x01) == 0x11
- fun bin_or(i: Int): Int `{ return self | i; `}
-
- # Alias of `bin_or`
- fun |(i: Int): Int do return bin_or(i)
+ # assert 0x10 | 0x01 == 0x11
+ fun |(i: Int): Int `{ return self | i; `}
# Returns the result of a binary XOR operation on `self` and `i`
#
- # assert 0x101.bin_xor(0x110) == 0x11
- fun bin_xor(i: Int): Int `{ return self ^ i; `}
-
- # Alias of `bin_xor`
- fun ^(i: Int): Int do return bin_xor(i)
+ # assert 0x101 ^ 0x110 == 0x11
+ fun ^(i: Int): Int `{ return self ^ i; `}
# Returns the 1's complement of `self`
#
- # assert 0x2F.bin_not == -48
- fun bin_not: Int `{ return ~self; `}
-
- # Alias of `bin_not`
- fun ~: Int do return bin_not
+ # assert ~0x2F == -48
+ fun ~: Int `{ return ~self; `}
# Returns the square root of `self`
#
if o < 0 then return -(self.gcd(-o))
if self == 0 or o == self then return o
if o == 0 then return self
- if self.bin_and(1) == 0 then
- if o.bin_and(1) == 1 then
- return self.rshift(1).gcd(o)
+ if self & 1 == 0 then
+ if o & 1 == 1 then
+ return (self >> 1).gcd(o)
else
- return self.rshift(1).gcd(o.rshift(1)).lshift(1)
+ return (self >> 1).gcd(o >> 1) << 1
end
end
- if o.bin_and(1) == 0 then return self.gcd(o.rshift(1))
- if self > o then return (self - o).rshift(1).gcd(o)
- return (o - self).rshift(1).gcd(self)
+ if o & 1 == 0 then return self.gcd(o >> 1)
+ if self > o then return ((self - o) >> 1).gcd(o)
+ return ((o - self) >> 1).gcd(self)
end
# Is `self` even ?
redef class Byte
# Returns the result of a binary AND operation on `self` and `i`
#
- # assert 0x10.bin_and(0x01) == 0
- fun bin_and(i: Byte): Byte `{ return self & i; `}
-
- # Alias of `bin_and`
- fun &(i: Byte): Byte do return bin_and(i)
+ # assert 0x10u8 & 0x01u8 == 0u8
+ fun &(i: Byte): Byte `{ return self & i; `}
# Returns the result of a binary OR operation on `self` and `i`
#
- # assert 0x10.bin_or(0x01) == 0x11
- fun bin_or(i: Byte): Byte `{ return self | i; `}
-
- # Alias of `bin_or`
- fun |(i: Byte): Byte do return bin_or(i)
+ # assert 0x10u8 | 0x01u8 == 0x11u8
+ fun |(i: Byte): Byte `{ return self | i; `}
# Returns the result of a binary XOR operation on `self` and `i`
#
- # assert 0x101.bin_xor(0x110) == 0x11
- fun bin_xor(i: Byte): Byte `{ return self ^ i; `}
-
- # Alias of `bin_xor`
- fun ^(i: Byte): Byte do return bin_xor(i)
+ # assert 0x101u8 ^ 0x110u8 == 0x11u8
+ fun ^(i: Byte): Byte `{ return self ^ i; `}
# Returns the 1's complement of `self`
#
- # assert 0x2F.bin_not == -48
- fun bin_not: Byte `{ return ~self; `}
-
- # Alias of `bin_not`
- fun ~: Byte do return bin_not
+ # assert ~0x2Fu8 == 0xD0u8
+ fun ~: Byte `{ return ~self; `}
end
redef class Float
fun round: Float `{ return round(self); `}
# Returns a random `Float` in `[0.0 .. self[`.
- fun rand: Float `{ return ((self)*rand())/(RAND_MAX+1.0); `}
+ fun rand: Float `{
+ if (nit_rand_seeded) return ((self)*nit_rand())/(NIT_RAND_MAX+1.0);
+ return ((self)*rand())/(RAND_MAX+1.0);
+ `}
# Returns the euclidean distance from `b`.
fun hypot_with(b: Float): Float `{ return hypotf(self, b); `}
# assert 10.rand == a
# assert 100.rand == b
# ~~~~
-fun srand_from(x: Int) `{ srand(x); `}
+fun srand_from(x: Int) `{ nit_rand_seeded = 1; nit_rand_seed = x; `}
# Reinitialize the pseudo-random generator used by the method `rand` and other.
# This method is automatically invoked at the begin of the program, so usually, there is no need to manually invoke it.
# The only exception is in conjunction with `srand_from` to reset the pseudo-random generator.
-fun srand `{ srand(time(NULL)); `}
+fun srand `{ nit_rand_seeded = 0; srand(time(NULL)); `}