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


Groups > comp.lang.c > #139132

Re: Multi-channel rand()

From "Rick C. Hodgin" <rick.c.hodgin@gmail.com>
Newsgroups comp.lang.c
Subject Re: Multi-channel rand()
Date 2018-12-27 11:40 -0500
Organization Liberty Software Foundation
Message-ID <q02v7q$fbr$1@dont-email.me> (permalink)
References (6 earlier) <q02o8d$2gl$1@dont-email.me> <q02oa9$2gl$2@dont-email.me> <4e3785c7-aae8-4f61-9ac7-8a6b34fce17d@googlegroups.com> <q02r0a$ldd$1@dont-email.me> <27896eaa-d407-4b10-9f00-8144e1e44f5b@googlegroups.com>

Show all headers | View raw


On 12/27/2018 11:22 AM, James Kuyper wrote:
> On Thursday, December 27, 2018 at 10:27:13 AM UTC-5, Rick C. Hodgin wrote:
>> On 12/27/2018 10:01 AM, James Kuyper wrote:
>>> On Thursday, December 27, 2018 at 9:41:20 AM UTC-5, Rick C. Hodgin wrote:
>>>> On 12/27/2018 9:41 AM, Rick C. Hodgin wrote:
> ...
>>>>> Given the implementation of the algorithm, I'm surprised they didn't
>>>>> provide a native one that uses pointers to external values so you can
>>>>> have multiple rand() seeds and sources all working together.
>>>
>>> If POSIX functions are an acceptable solution, then, as Scott has
>>> already pointed out, the initstate() and setstate() functions can be
>>> used to make random() use different sets of state information, totally
>>> under your control. You responded to his message with a message about a
>>> Windows function, so it's not clear whether you noticed that part of
>>> what he was saying.
>>
>> I did.  It seems an undesirable solution due to the spill/fill overhead,
>> so I discounted it as a viable solution for me.
>>
>> I was able to get the solution working based on Chris Thomasson's link
>> to the Permuted Congruential Generator (say that five times fast).
>>
>>       https://en.wikipedia.org/wiki/Permuted_congruential_generator
>>
>> The code there can be modified easily to point to inputs for its "in-
>> ternal" values.  No spill/fill required.
>
> I don't understand the spill/fill problem you're thinking of. Let me
> give an example, and then you can use that example to show me where the
> spilling and filling that is bothering you occurs:
> 
>      #define CHANNELS 25
>      #define RANDOM_STATE_SIZE 64
>      char random_state[CHANNELS][RANDOM_STATE_SIZE];
> 
> At startup:
>      initstate(seed, random_state[channel],
>          sizeof random_state[channel]);
> 
> When random numbers are needed for a given channel:
> 
>        setstate(random_state[channel]);
>        for(die = 0; die < num_dice; die++)
>            dice[die] = random()*6/0x8000000;


I did not investigate initstate() and setstate().  I assumed they
were copying external values into the internal state used by the
rand() function, so that rand() would work on its own internal
variables.  What you show above indicates it stores pointers to
the external data and is similar to what I was searching for.

I would prefer to use similar to as you indicate below:

     // An actual type called random_state which includes every-
     // thing it needs for its internal processing.
     random_state rs[CHANNELS];

     // Initialize them at startup
     for (i = 0; i < CHANNELS; ++i)
         initstate(seed, &rs[i]);

     // Use them in code as needed
     whatever = random(&rs[CHANNEL_2]);

Although in reality I would have it be like this:

     random_state input1;
     random_state input2;

And would use them in code:

     whatever1 = random(&input1);
     whatever2 = random(&input2);

> I'd guessed from your original question that you were thinking of
> something like this:
> 
>      random_handle_type handle[CHANNELS];
> 
> At startup:
>      handle[channel] = initialize_random_generator(seed);
> 
> When random numbers are needed:
>      for(die=0; die < num_dice; die++)
>          dice[die] = random_number(handle[channel]);
> 
> Please note that what random_number(handle[channel]) does internally
> with it's handle[channel] might be exactly the same as what
> set_state(random_state[channel]) does with random_state[channel].

Very similar.

I forget sometimes that C doesn't require explicit casting. :-)
There are times I miss that using my C++ compiler for mostly C
code.  It's one of the things I allow in CAlive (an option to
turn off explicit cast requirements), and I also create a new
void type called "any" that allows casting to any other type
without needing to have an explicit cast if you keep explicit
cast requirements enabled.

-- 
Rick C. Hodgin

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