Mirsad Todorovac wrote:
Considering use of Bar[j], here is a method of getting a fair 0.5 probability of each bit being set:
m := MaxN div SetWordSize - 1; for j := 0 to m do Bar[j] := Random (High (MedCard));
However, this also relies on Random () providing pseudo-random numbers of good quality, with 0.5 probability of each bit being set. True, it's also much faster than initializing bit by bit via Include (SetA, Random(MaxN)).
As I said, you can achieve this also without relying on the internal representation.
Loop over the set range, and for each element, if Random (2) = 0 (e.g.), include it. This can be adapted easily for other probabilities.
For efficiency, you can get a 32 bit random number and use it to set 32 elements. At least for p = 0.5 (for other probabilities, it can perhaps be adjusted, e.g., using 2 bits per element for p = 0.25 or p = 0.75).
We could read from some real random source if we need truly random numbers, such as required in security, but I think Random() function suffices here, wouldn't you agree?
Surely. You need real random for cryptography etc., not for these things. The additional complication (since not every system has a readily available source of real random, to start with) is not worth it.
Frank