Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #139084

Re: Multi-channel rand()

Subject Re: Multi-channel rand()
Newsgroups comp.lang.c
References <pvrv1d$4ls$1@dont-email.me> <bpNUD.21754$X_3.4267@fx01.am4> <c720a209-9a04-47ab-b9e1-29b1b570231a@googlegroups.com>
From Richard Damon <Richard@Damon-Family.org>
Message-ID <6yOUD.36923$Bu5.21082@fx19.iad> (permalink)
Organization Forte - www.forteinc.com
Date 2018-12-26 12:06 -0500

Show all headers | View raw


On 12/26/18 10:58 AM, Rick C. Hodgin wrote:
> On Wednesday, December 26, 2018 at 10:48:31 AM UTC-5, Bart wrote:
>> On 25/12/2018 00:54, Rick C. Hodgin wrote:
>>> Is there an equivalent of a multi-channel rand() function that enables
>>> multiple simultaneous separate pseudo-random number generators by some-
>>> thing like a handle or instance of each in the standard library?
>>
>> This doesn't seem too hard, but it's obviously not in the standard 
>> library (which AFAIK only has rand()). Example:
>>
>> -------------------
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <stdint.h>
>>
>> typedef uint64_t u64;
>> typedef u64* Randgen;
>>
>> Randgen new_randgen(u64 seed1, u64 seed2) {
>> // needs two 64-bit non-zero seeds
>>      Randgen s;
>>
>>      s=malloc(sizeof(u64)*2);
>>      if (s==NULL) {
>>          return s;
>>      }
>>      s[0]=seed1;
>>      s[1]=seed2;
>>      return s;
>> }
>>
>> void free_randgen(Randgen s) {
>>      free(s);
>> }
>>
>> u64 random(Randgen s) {
>> // based on 'xorshift128+'
>>      u64 x = s[0];
>>      u64 const y = s[1];
>>      s[0] = y;
>>      x ^= x << 23; // a
>>      s[1] = x ^ y ^ (x >> 17) ^ (y >> 26); // b, c
>>      return s[1] + y;
>> }
>>
>> int main(void) {
>>      u64 a;
>>      Randgen r1,r2,r3;
>>
>>      r1=new_randgen(374646464,84747774);
>>      r2=new_randgen(374646464,84747774);
>>      r3=new_randgen(374646464,84747774);
>>
>>      for (int i=0; i<10; ++i) {
>>          printf("%d %016llX %016llX %016llX\n",i,
>>              random(r1), random(r2), random(r3));
>>      }
>>
>>      free_randgen(r1);
>>      free_randgen(r2);
>>      free_randgen(r3);
>>
>> }
>> -----------------------
>>
>> All three streams show the same results because I've used the same seed 
>> for each.
>>
>> (To reset a stream R to a new seed, either free R and set R to a new 
>> generator, or change the two u64s that R points to, or provide a 
>> function for that.
>>
>> However this depends entirely on the PRNG used. The point of my example 
>> is to show how easy it is to superimpose a 'multi-channel' API on a 
>> single-stream PRNG.)
> 
> I agree.  That's what I intended to do, but I didn't know where
> there was good prng source code that was in the Public Domain.
> 
> I'm looking for a nicely distributed prng.  I'm using the built-
> in one with Visual C++ presently and it has really odd patterns
> which, when scaled into a lesser range (like 1-10), produce oft-
> repeated values.  I'll have to add logic to prohibit too many of
> any single integer appearing in a row, or over a short span, as
> there are times it will return four mapped to the same integer
> in the 1..10 range in a row.  That's not acceptable for this
> type of game's needs.  It also sometimes has an affinity for a
> certain set of values, like it will produce a lot of 3s, 5s, 8s,
> and 9s, and very few 1s, 4s, 6s, and an average amount of 2s,
> 7s, and 10s, for example.  Weird.
> 
> I was able to get my random seeding working properly.  It uses
> user interaction time coupled with computer system time, and
> takes multiple samples during game setup, and uses those values.
> It's seeding things nicely.
> 

Beware of trying to force randomization by removing 'obviously
non-random' sequences, because ALL specific pattern should be equally
likely, so 4 6s in a row (from a 1-10 draw) should happen once every
10,000 numbers, so removing it makes the results LESS random.

Also, given a given random number generator, there are better and worse
ways to extract a random subrange. For many generators, using something
like mod or anding the lower bits is very bad, as the lower bits are
less random than upper bits.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-24 19:54 -0500
  Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-24 19:38 -0800
  Re: Multi-channel rand() nobody@example.org (Scott) - 2018-12-25 04:05 +0000
    Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-24 23:57 -0500
      Re: Multi-channel rand() Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2018-12-25 09:22 +0000
        Professional coders??? (Was: Multi-channel rand()) gazelle@shell.xmission.com (Kenny McCormack) - 2018-12-25 10:09 +0000
        Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-25 07:59 -0500
          Re: Multi-channel rand() Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2018-12-25 21:59 +0000
            Re: Multi-channel rand() nobody@example.org (Scott) - 2018-12-26 02:43 +0000
      Re: Multi-channel rand() rbowman <bowman@montana.com> - 2018-12-25 10:15 -0700
        Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-25 10:14 -0800
  Re: Multi-channel rand() Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-12-25 13:35 +0300
    Re: Multi-channel rand() fir <profesor.fir@gmail.com> - 2018-12-25 02:56 -0800
    Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-25 08:02 -0500
  Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-25 11:52 +0000
    Re: Multi-channel rand() Anton Shepelev <anton.txt@g{oogle}mail.com> - 2018-12-25 14:58 +0300
    Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-25 06:59 -0500
      Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-25 08:05 -0500
    Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-25 08:04 -0500
  Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-26 15:48 +0000
    Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 07:58 -0800
      Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 12:06 -0500
        Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 12:45 -0500
          Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 13:20 -0500
            Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 10:30 -0800
              Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-26 11:35 -0800
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 14:52 -0500
              Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 16:31 -0500
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 22:28 -0500
                Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-30 20:42 -0800
            Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-26 15:32 -0800
          Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-26 19:38 +0000
            Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-26 15:35 -0800
        Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 12:59 -0500
          Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 16:05 -0500
        Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 13:08 -0500
          Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 15:50 -0500
      Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-26 17:42 +0000
        Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 12:48 -0500
        Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 12:58 -0500
          Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-26 19:03 +0000
            Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 14:45 -0500
              Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-26 20:46 +0000
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 16:25 -0500
                Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 17:13 -0500
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-26 22:23 -0500
                Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-26 23:39 +0000
                Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 17:36 -0500
            Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 16:27 -0500
    Re: Multi-channel rand() rbowman <bowman@montana.com> - 2018-12-26 19:19 -0700
      Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-26 22:17 -0500
      Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-26 19:32 -0800
      Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-26 19:34 -0800
        Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-27 11:29 +0000
          Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 05:27 -0800
            Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 09:41 -0500
              Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 09:42 -0500
                Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 07:01 -0800
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 10:28 -0500
                Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-27 10:48 -0500
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 11:08 -0500
                Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-27 13:49 -0800
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 17:18 -0500
                Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-29 13:59 -0800
                Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-30 14:01 -0800
                Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-30 22:07 +0000
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-30 17:20 -0500
                Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-27 17:47 -0500
                Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-30 20:45 -0800
                Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 08:22 -0800
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 11:40 -0500
                Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 09:21 -0800
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 12:45 -0500
                Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 10:57 -0800
                Re: Multi-channel rand() "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2018-12-27 14:35 -0500
                Re: Multi-channel rand() Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 09:25 -0800
                Re: Multi-channel rand() Tim Rentsch <txr@alumni.caltech.edu> - 2018-12-28 09:55 -0800
                Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-28 12:36 -0800
                Re: Multi-channel rand() Robert Wessel <robertwessel2@yahoo.com> - 2018-12-27 20:34 -0600
              Re: Multi-channel rand() Richard Damon <Richard@Damon-Family.org> - 2018-12-27 10:37 -0500
            Re: Multi-channel rand() Bart <bc@freeuk.com> - 2018-12-27 14:46 +0000
              Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 07:05 -0800
                Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-27 11:28 -0800
                Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 12:07 -0800
                Re: Multi-channel rand() Keith Thompson <kst-u@mib.org> - 2018-12-27 12:47 -0800
                Re: Multi-channel rand() James Kuyper <jameskuyper@alumni.caltech.edu> - 2018-12-27 14:47 -0800
  Re: Multi-channel rand() "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2018-12-26 11:58 -0800

csiph-web