Urban Hafner wrote:
Hej,
I need to create random bits and XOR them together. For creating random
numbers there is OS.rand so I at first figured I'd use that and then use
and xor function on them. But there doesn't seem to be such a function.

Then I thought I could use BitStrings. There is a bitwise or for
bitstrings, but I don't know if it is exclusive or inclusive. And then
there is the problem with generating random bitstrings.

BitString.disj implements inclusive or. Another undocumented feature is that indices start at 0 (stupid choice IMHO).

Here is an idea for generating random bitstring. I assume that digits 0 and 1 have same probability. I determine the indices of digits 1 in the bitstring by filtering the list of indices, randomly choosing to keep them or not.

Cheers,
raph


%% returns true or false (each with probability .5)
fun {RandomCond _} {OS.rand} mod 2 > 0 end

%% returns a random bitstring of length I
fun {RandomBitString I}
   {BitString.make I {Filter {List.number 0 I-1 1} RandomCond}}
end

%% bitwise XOR between bitstrings
fun {BitwiseXOR BS1 BS2}
   %% a xor b == (a and (not b)) or ((not a) and b)
   {BitString.disj
    {BitString.conj BS1 {BitString.nega BS2}}
    {BitString.conj {BitString.nega BS1} BS2}}
end

_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to