home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: sci.crypt
- Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!slc3.ins.cwru.edu!agate!linus!linus.mitre.org!news
- From: lewis@aera8700.mitre.org (Keith Lewis)
- Subject: Re: random values
- Message-ID: <1992Aug21.134217.23612@linus.mitre.org>
- Sender: news@linus.mitre.org (News Service)
- Nntp-Posting-Host: aera8700.mitre.org
- Organization: The MITRE Corporation
- References: <1992Aug20.864.127@ALMAC>
- Date: Fri, 21 Aug 1992 13:42:17 GMT
- Lines: 29
-
- In article <1992Aug20.864.127@ALMAC>, keith.willis@almac.co.uk writes:
- > This is fine as a random bitstream, but for his application,
- > he needs a set of random _values_, in a defined range, with
-
- You can convert from a random bitstream to a stream of random discrete
- values (let's say, integers from 0..R) by the following algorithm.
-
- Determine N, the number of bits you need (lowest N such that 2**N > R)
-
- while (more bits available)
- construct integer X out of next N bits (X = b(1)+2*b(2)+4*b(3)+...)
- if X <= R
- output X to stream
- else
- do nothing (discard bad X)
- endwhile
-
- For ranges that are exactly a power of two, this algorithm works perfectly.
- For other ranges, it may discard (waste) almost half your bitstream.
-
- > the constraint that there must be at least one of each
- > possible value.
-
- At least one of each possible value? That's a weird requirement. Could you
- just add a list of all possible values onto the end of the stream?
-
- Keith Lewis klewis@mitre.org "Mr. Cheap"
- I don't dance to music; music dances to me. Email me for my PGP key.
- The above may not (yet) represent the opinions of my employer.
-