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


Groups > comp.lang.c > #139043 > unrolled thread

Multi-channel rand()

Started by"Rick C. Hodgin" <rick.c.hodgin@gmail.com>
First post2018-12-24 19:54 -0500
Last post2018-12-26 11:58 -0800
Articles 7 on this page of 87 — 14 participants

Back to article view | Back to comp.lang.c


Contents

  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

Page 5 of 5 — ← Prev page 1 2 3 4 [5]


#139122

FromBart <bc@freeuk.com>
Date2018-12-27 14:46 +0000
Message-ID<fB5VD.174539$krL1.147896@fx15.am4>
In reply to#139119
On 27/12/2018 13:27, James Kuyper wrote:
> On Thursday, December 27, 2018 at 6:29:18 AM UTC-5, Bart wrote:
> ...
>> Also the fact that rand() doesn't support the 'multi-channel' requirement.
>>
>> The Posix set of functions might enable that, but it's difficult to
>> tell. If they do, then they rely on global state which is fiddly to
>> change; it might be better to apply an API like mine.
>>
>> -- 
>> bart
> 
> The documentation for random() says:
> "Threaded applications should use erand48(), nrand48(), or jrand48()
> instead of random() when an independent random number sequence in
> multiple threads is required."
> 

OK (it sounds like random() doesn't support independent sequences).

My on-line docs say to use the random_r() set of functions for multiple 
threads. But that's more complex and with the result returned 
differently (via a pointer), and the N independent streams need separate 
arrangements to set up.

(I don't know if the OP's requirements, and those of multi-threading, 
are the same thing. Although the OP's focus seems to have shifted 
towards random number sequences with distributions that are compatible 
with his application.)

I think that if the global state used by a PRNG can be reliably 
isolated, then an approach like the one I suggested earlier in the 
thread would work better and be simpler and (for that specific prng) be 
more portable. It might even work with multiple threads.

-- 
bart

[toc] | [prev] | [next] | [standalone]


#139124

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2018-12-27 07:05 -0800
Message-ID<ea2a9290-0ffc-4b50-a6c7-19c89e9691df@googlegroups.com>
In reply to#139122
On Thursday, December 27, 2018 at 9:46:42 AM UTC-5, Bart wrote:
> On 27/12/2018 13:27, James Kuyper wrote:
> > On Thursday, December 27, 2018 at 6:29:18 AM UTC-5, Bart wrote:
> > ...
> >> Also the fact that rand() doesn't support the 'multi-channel' requirement.
> >>
> >> The Posix set of functions might enable that, but it's difficult to
> >> tell. If they do, then they rely on global state which is fiddly to
> >> change; it might be better to apply an API like mine.
> >>
> >> -- 
> >> bart
> > 
> > The documentation for random() says:
> > "Threaded applications should use erand48(), nrand48(), or jrand48()
> > instead of random() when an independent random number sequence in
> > multiple threads is required."
> > 
> 
> OK (it sounds like random() doesn't support independent sequences).

It does, through the initstate() and setstate() functions.

[toc] | [prev] | [next] | [standalone]


#139138

FromKeith Thompson <kst-u@mib.org>
Date2018-12-27 11:28 -0800
Message-ID<ln1s62bwgb.fsf@kst-u.example.com>
In reply to#139124
James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> On Thursday, December 27, 2018 at 9:46:42 AM UTC-5, Bart wrote:
[...]
>> OK (it sounds like random() doesn't support independent sequences).
>
> It does, through the initstate() and setstate() functions.

Yes, but you'd have to save and restore the entire state on each
call, or at least each time you want to switch to a different
sequence.  The [enr]rand48() functions take a pointer to the state
(a 48-bit integer stored as an array of 3 unsigned shorts) as
an argument.

(Probably if they were designed today, they'd use an unsigned long
long to hold the state.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#139140

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2018-12-27 12:07 -0800
Message-ID<df724ae4-87ec-4a67-9d0e-779098a5e2de@googlegroups.com>
In reply to#139138
On Thursday, December 27, 2018 at 2:29:01 PM UTC-5, Keith Thompson wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> > On Thursday, December 27, 2018 at 9:46:42 AM UTC-5, Bart wrote:
> [...]
> >> OK (it sounds like random() doesn't support independent sequences).
> >
> > It does, through the initstate() and setstate() functions.
> 
> Yes, but you'd have to save and restore the entire state on each
> call, or at least each time you want to switch to a different
> sequence.

Was the example code I gave to Rick coded incorrectly? This is a feature
I've never personally used, so I may have made a mistake, but that code
did not save or restore any state, because the interface doesn't seem to
require doing so.

[toc] | [prev] | [next] | [standalone]


#139141

FromKeith Thompson <kst-u@mib.org>
Date2018-12-27 12:47 -0800
Message-ID<lnwonuae8f.fsf@kst-u.example.com>
In reply to#139140
James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> On Thursday, December 27, 2018 at 2:29:01 PM UTC-5, Keith Thompson wrote:
>> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>> > On Thursday, December 27, 2018 at 9:46:42 AM UTC-5, Bart wrote:
>> [...]
>> >> OK (it sounds like random() doesn't support independent sequences).
>> >
>> > It does, through the initstate() and setstate() functions.
>> 
>> Yes, but you'd have to save and restore the entire state on each
>> call, or at least each time you want to switch to a different
>> sequence.
>
> Was the example code I gave to Rick coded incorrectly? This is a feature
> I've never personally used, so I may have made a mistake, but that code
> did not save or restore any state, because the interface doesn't seem to
> require doing so.

I was wrong.  The initstate() function returns a pointer to the state
(as a char*), and setstate() returns a pointer to the previous state.

I'm going to assume your example code was probably correct, but I didn't
save a copy.

So if you want multiple streams using random(), you could do something
like:

    setstate(pointer_to_state_for_this_thread);
    long int r = random();

But if you want a distinct stream for each thread, you'd have to use
some kind of synchronization to ensure that there's no context switch
between the setstate() and random() calls.

Since I misunderstood the state management the last time I read the man
page, it's entirely possible that I've misunderstood it again in some
new and embarrassing way.

-- 
Keith Thompson (The_Other_Keith) kst@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#139144

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2018-12-27 14:47 -0800
Message-ID<45fb8440-03ce-4382-a0f1-2a7d85ddfb3c@googlegroups.com>
In reply to#139141
On Thursday, December 27, 2018 at 3:47:52 PM UTC-5, Keith Thompson wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> > On Thursday, December 27, 2018 at 2:29:01 PM UTC-5, Keith Thompson wrote:
> >> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> >> > On Thursday, December 27, 2018 at 9:46:42 AM UTC-5, Bart wrote:
> >> [...]
> >> >> OK (it sounds like random() doesn't support independent sequences).
> >> >
> >> > It does, through the initstate() and setstate() functions.
> >> 
> >> Yes, but you'd have to save and restore the entire state on each
> >> call, or at least each time you want to switch to a different
> >> sequence.
> >
> > Was the example code I gave to Rick coded incorrectly? This is a feature
> > I've never personally used, so I may have made a mistake, but that code
> > did not save or restore any state, because the interface doesn't seem to
> > require doing so.
> 
> I was wrong.  The initstate() function returns a pointer to the state
> (as a char*), and setstate() returns a pointer to the previous state.

Actually, both functions return a pointer to the previous state.
That you made that mistake suggests to me that you might be thinking that initstate() allocates memory for the state array. As I understand it, it uses the memory provided to it by the user - the user is responsible for allocation of that memory.

> I'm going to assume your example code was probably correct, but I didn't
> save a copy.
> 
> So if you want multiple streams using random(), you could do something
> like:
> 
>     setstate(pointer_to_state_for_this_thread);
>     long int r = random();
> 
> But if you want a distinct stream for each thread, you'd have to use
> some kind of synchronization to ensure that there's no context switch
> between the setstate() and random() calls.
> 
> Since I misunderstood the state management the last time I read the man
> page, it's entirely possible that I've misunderstood it again in some
> new and embarrassing way.

I think you may have. However, you didn't state your understanding in enough detail for me to be sure.

[toc] | [prev] | [next] | [standalone]


#139098

From"Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid>
Date2018-12-26 11:58 -0800
Message-ID<q00mga$tb$1@dont-email.me>
In reply to#139043
On 12/24/2018 4:54 PM, 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?
> 

Fwiw, take a deep look at:

https://en.wikipedia.org/wiki/Linear_congruential_generator

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

Back to top | Article view | comp.lang.c


csiph-web