Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #88434 > unrolled thread
| Started by | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| First post | 2023-01-08 06:01 +0100 |
| Last post | 2023-01-13 05:04 -0800 |
| Articles | 14 on this page of 74 — 15 participants |
Back to article view | Back to comp.lang.c++
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-08 06:01 +0100
Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-08 14:48 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-08 18:22 +0100
Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-08 17:46 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-09 04:58 +0100
Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-09 11:26 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-09 15:57 +0100
Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-08 17:34 +0000
Re: Compute Unique Numbers in a Set Ike Naar <ike@sdf.org> - 2023-01-08 21:45 +0000
Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-08 23:13 +0000
Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-01-08 15:18 -0800
Re: Compute Unique Numbers in a Set Tim Woodall <news001@woodall.me.uk> - 2023-01-13 06:35 +0000
Re: Compute Unique Numbers in a Set Tim Woodall <news001@woodall.me.uk> - 2023-01-13 06:39 +0000
Re: Compute Unique Numbers in a Set Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-08 21:19 +0200
Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-08 19:20 +0000
Re: Compute Unique Numbers in a Set "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-01-09 12:03 +0100
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-09 17:42 +0100
Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-09 23:22 +0000
Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-10 05:11 -0800
Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-10 05:19 -0800
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-13 08:26 +0100
Re: Compute Unique Numbers in a Set Öö Tiib <ootiib@hot.ee> - 2023-01-13 03:26 -0800
Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-13 12:21 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-13 14:11 +0100
Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-13 13:55 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-13 15:02 +0100
Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-13 14:17 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-15 16:06 +0100
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-15 16:47 +0100
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-15 17:13 +0100
Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-15 08:57 -0800
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-16 09:46 +0100
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-16 10:21 +0100
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-16 13:35 +0100
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-16 14:46 +0100
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-16 14:42 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-16 16:38 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-16 21:06 +0100
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-17 09:19 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-17 09:30 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-17 14:14 +0100
Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-17 07:48 -0800
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-17 16:49 +0100
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-17 17:32 +0100
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-17 19:24 +0100
Re: Compute Unique Numbers in a Set Ralf Goertz <me@myprovider.invalid> - 2023-01-17 17:31 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-17 17:10 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-17 18:18 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-18 09:23 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-18 13:31 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-18 16:16 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-18 17:20 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-18 16:24 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-18 17:59 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-18 17:14 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-18 18:23 +0100
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-19 09:31 +0000
Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-01-17 12:48 -0800
Re: Compute Unique Numbers in a Set scott@slp53.sl.home (Scott Lurndal) - 2023-01-17 21:21 +0000
Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-01-17 13:29 -0800
Re: Compute Unique Numbers in a Set Paul N <gw7rib@aol.com> - 2023-01-18 06:50 -0800
Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-01-18 11:59 -0800
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-17 16:25 +0000
Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-17 09:08 -0800
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-17 17:16 +0000
Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-17 17:32 +0000
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-18 09:23 +0000
Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-18 12:26 +0000
Re: Compute Unique Numbers in a Set Muttley@dastardlyhq.com - 2023-01-18 16:09 +0000
Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-15 16:27 +0000
Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-15 17:42 +0100
Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-15 17:11 +0000
Re: Compute Unique Numbers in a Set Tim Woodall <news001@woodall.me.uk> - 2023-01-13 06:32 +0000
Re: Compute Unique Numbers in a Set Öö Tiib <ootiib@hot.ee> - 2023-01-13 05:04 -0800
Page 4 of 4 — ← Prev page 1 2 3 [4]
| From | Paul N <gw7rib@aol.com> |
|---|---|
| Date | 2023-01-18 06:50 -0800 |
| Message-ID | <43d56162-d8e1-47b8-b80e-5c5fd5012a78n@googlegroups.com> |
| In reply to | #88577 |
On Tuesday, January 17, 2023 at 9:29:16 PM UTC, Chris M. Thomasson wrote: > On 1/17/2023 1:21 PM, Scott Lurndal wrote: > > "Chris M. Thomasson" <chris.m.t...@gmail.com> writes: > >> On 1/17/2023 7:49 AM, Bonita Montero wrote: > >>> Am 17.01.2023 um 16:48 schrieb Malcolm McLean: > >>> > >>>> A shuffle followed by taking the first elements of the vector isn't a > >>>> particularly > >>>> efficient way of generating a sequence of unique random numbers. But it's > >>>> not all that ineffieicnt either, and there's often an advantage in > >>>> writing something > >>>> simply ans quickly from pre-existing components, rather than writing a > >>>> tailor- > >>>> made, customised solution. > >>> > >>> You don't have real randomness by shuffling. > >>> > >> > >> Are you saying that there is no real randomness wrt shuffling a deck of > >> cards around seven times in a row? > > > > That depends on how it is shuffled. > > > > https://en.wikipedia.org/wiki/Faro_shuffle > Touche. How about riffle shuffles? I think a Faro shuffle is just a perfect riffle shuffle. 8 out-shuffles or 52 in-shuffles put the deck back into its original order. Also, any sort of shuffle based on a 32-bit seed will give one of about 4 billion orders for the cards. This sounds a lot but is dwarfed by the 52! possible orders, and it means that once you've seen about 6 or 7 of the cards you can work out exactly what all the others are.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2023-01-18 11:59 -0800 |
| Message-ID | <tq9j3a$11aca$1@dont-email.me> |
| In reply to | #88600 |
On 1/18/2023 6:50 AM, Paul N wrote: > On Tuesday, January 17, 2023 at 9:29:16 PM UTC, Chris M. Thomasson wrote: >> On 1/17/2023 1:21 PM, Scott Lurndal wrote: >>> "Chris M. Thomasson" <chris.m.t...@gmail.com> writes: >>>> On 1/17/2023 7:49 AM, Bonita Montero wrote: >>>>> Am 17.01.2023 um 16:48 schrieb Malcolm McLean: >>>>> >>>>>> A shuffle followed by taking the first elements of the vector isn't a >>>>>> particularly >>>>>> efficient way of generating a sequence of unique random numbers. But it's >>>>>> not all that ineffieicnt either, and there's often an advantage in >>>>>> writing something >>>>>> simply ans quickly from pre-existing components, rather than writing a >>>>>> tailor- >>>>>> made, customised solution. >>>>> >>>>> You don't have real randomness by shuffling. >>>>> >>>> >>>> Are you saying that there is no real randomness wrt shuffling a deck of >>>> cards around seven times in a row? >>> >>> That depends on how it is shuffled. >>> >>> https://en.wikipedia.org/wiki/Faro_shuffle >> Touche. How about riffle shuffles? > > I think a Faro shuffle is just a perfect riffle shuffle. 8 out-shuffles or 52 in-shuffles put the deck back into its original order. I do not want a perfect shuffle. > Also, any sort of shuffle based on a 32-bit seed will give one of about 4 billion orders for the cards. This sounds a lot but is dwarfed by the 52! possible orders, and it means that once you've seen about 6 or 7 of the cards you can work out exactly what all the others are. How do the Casinos handle it?
[toc] | [prev] | [next] | [standalone]
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2023-01-17 16:25 +0000 |
| Message-ID | <tq6i65$b6g$1@gioia.aioe.org> |
| In reply to | #88560 |
On Tue, 17 Jan 2023 07:48:02 -0800 (PST) Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote: >On Tuesday, 17 January 2023 at 13:13:37 UTC, Bonita Montero wrote: >> If you want the code to be as short and as performant >> as possible there's no way to program different. >> >A shuffle followed by taking the first elements of the vector isn't a >particularly >efficient way of generating a sequence of unique random numbers. But it's Just out of interest, what would be a better way? You could randomly select elements in a container then delete that particular element so it doesn't get used again but I'm not sure that would be very efficient beyond single digit container sizes. Ditto starting at a random point in the container and walking it in a random direction until you find an unused element.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.arthur.mclean@gmail.com> |
|---|---|
| Date | 2023-01-17 09:08 -0800 |
| Message-ID | <f9d7af32-c89e-4f03-968c-0c3d67a0cdd2n@googlegroups.com> |
| In reply to | #88564 |
On Tuesday, 17 January 2023 at 16:25:56 UTC, Mut...@dastardlyhq.com wrote: > On Tue, 17 Jan 2023 07:48:02 -0800 (PST) > Malcolm McLean <malcolm.ar...@gmail.com> wrote: > >On Tuesday, 17 January 2023 at 13:13:37 UTC, Bonita Montero wrote: > >> If you want the code to be as short and as performant > >> as possible there's no way to program different. > >> > >A shuffle followed by taking the first elements of the vector isn't a > >particularly > >efficient way of generating a sequence of unique random numbers. But it's > Just out of interest, what would be a better way? You could randomly select > elements in a container then delete that particular element so it doesn't get > used again but I'm not sure that would be very efficient beyond single digit > container sizes. Ditto starting at a random point in the container and walking > it in a random direction until you find an unused element. > You have the range 0 to N-1 to choose M unique elements from. Pick a single element x using a unifrom random number. That now gives us two ranges, 0 to x-1 and x+1 to N-1. And M-1 numbers left to pick. The number we need to pick from each range is given by the hypergeometric distribution. We have x "good" balls and N - x - 2 "bad" balls in an urn, and we withdraw M-1 balls. The number of "good" balls we draw tells us how many elemets to distribute to the first half of the range. The remainder of the elements we choose from the second half of the range. Then it's just recursive, with the range splitting on a random elemnet each level. You cna write a generator for numbers with a hypergeometric distribution naively and quite simply by taking M random numbers. Or you can do it in a far more complicated way by calculating the cumulative distribution function. If you do it the naive way, the algorithm is O(M log M) (M calls to the uniform function on each level, and log M levels of recursion). If you do it the complicated way you can improve on that.
[toc] | [prev] | [next] | [standalone]
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2023-01-17 17:16 +0000 |
| Message-ID | <tq6l4j$1sr4$1@gioia.aioe.org> |
| In reply to | #88567 |
On Tue, 17 Jan 2023 09:08:12 -0800 (PST) Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote: >On Tuesday, 17 January 2023 at 16:25:56 UTC, Mut...@dastardlyhq.com wrote: >> Just out of interest, what would be a better way? You could randomly select >> elements in a container then delete that particular element so it doesn't >get >> used again but I'm not sure that would be very efficient beyond single digit > >> container sizes. Ditto starting at a random point in the container and >walking >> it in a random direction until you find an unused element. >> >You have the range 0 to N-1 to choose M unique elements from. >Pick a single element x using a unifrom random number. That now gives >us two ranges, 0 to x-1 and x+1 to N-1. And M-1 numbers left to pick. > >The number we need to pick from each range is given by the hypergeometric >distribution. We have x "good" balls and N - x - 2 "bad" balls in an urn, and >we withdraw M-1 balls. The number of "good" balls we draw tells us how many >elemets to distribute to the first half of the range. The remainder of the >elements > we choose from the second half of the range. >Then it's just recursive, with the range splitting on a random elemnet each >level. > >You cna write a generator for numbers with a hypergeometric distribution >naively >and quite simply by taking M random numbers. Or you can do it in a far more >complicated >way by calculating the cumulative distribution function. If you do it the >naive way, >the algorithm is O(M log M) (M calls to the uniform function on each level, >and log M >levels of recursion). If you do it the complicated way you can improve on that. Probably looks good on a whiteboard but there's a lot of shifting stuff about going on there so I doubt it'll be any faster (or more random) than simply doing random walks of an array until you find an unused element. But then its obviously based on the same algo as quicksort and selecting the optimal sorting function is a black art when copy costs vs comparison costs are factored in.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2023-01-17 17:32 +0000 |
| Message-ID | <878ri12hjh.fsf@bsb.me.uk> |
| In reply to | #88564 |
Muttley@dastardlyhq.com writes: > On Tue, 17 Jan 2023 07:48:02 -0800 (PST) > Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote: >>On Tuesday, 17 January 2023 at 13:13:37 UTC, Bonita Montero wrote: >>> If you want the code to be as short and as performant >>> as possible there's no way to program different. >>> >>A shuffle followed by taking the first elements of the vector isn't a >>particularly >>efficient way of generating a sequence of unique random numbers. But it's > > Just out of interest, what would be a better way? "Better" depends on the context. This thread has been split between comp.lang.c and comp.lang.c++ and if you have not been reading both you won't have seen all the many proposed algorithms. There are lots of possible trade-offs between the number of random number call, the amount of storage needed and sizes of the numbers involved. It isn't even clear what's wanted. The OP might have wanted just a random selection (all possible subsets equally probable) they might have wanted a random permutation (where all possible sequences of all possible subsets are equally probable). -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2023-01-18 09:23 +0000 |
| Message-ID | <tq8dr8$ctf$1@gioia.aioe.org> |
| In reply to | #88571 |
On Tue, 17 Jan 2023 17:32:34 +0000 Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: >Muttley@dastardlyhq.com writes: > >> On Tue, 17 Jan 2023 07:48:02 -0800 (PST) >> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote: >>>On Tuesday, 17 January 2023 at 13:13:37 UTC, Bonita Montero wrote: >>>> If you want the code to be as short and as performant >>>> as possible there's no way to program different. >>>> >>>A shuffle followed by taking the first elements of the vector isn't a >>>particularly >>>efficient way of generating a sequence of unique random numbers. But it's >> >> Just out of interest, what would be a better way? > >"Better" depends on the context. This thread has been split between >comp.lang.c and comp.lang.c++ and if you have not been reading both you I wish people wouldn't do that, its very annoying.
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2023-01-18 12:26 +0000 |
| Message-ID | <tq8oid$2o73d$1@news.xmission.com> |
| In reply to | #88595 |
In article <tq8dr8$ctf$1@gioia.aioe.org>, <Muttley@dastardlyhq.com> wrote:
...
>>"Better" depends on the context. This thread has been split between
>>comp.lang.c and comp.lang.c++ and if you have not been reading both you
>
>I wish people wouldn't do that, its very annoying.
But it is the correct thing. Annoying (to you) or not.
--
Nov 4, 2008 - the day when everything went
from being Clinton's fault to being Obama's fault.
[toc] | [prev] | [next] | [standalone]
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2023-01-18 16:09 +0000 |
| Message-ID | <tq95ju$1g6u$1@gioia.aioe.org> |
| In reply to | #88597 |
On Wed, 18 Jan 2023 12:26:53 -0000 (UTC) gazelle@shell.xmission.com (Kenny McCormack) wrote: >In article <tq8dr8$ctf$1@gioia.aioe.org>, <Muttley@dastardlyhq.com> wrote: >.... >>>"Better" depends on the context. This thread has been split between >>>comp.lang.c and comp.lang.c++ and if you have not been reading both you >> >>I wish people wouldn't do that, its very annoying. > >But it is the correct thing. Annoying (to you) or not. Says who, you? Why split a thread that is as relevant (or not) for both groups?
[toc] | [prev] | [next] | [standalone]
| From | Bart <bc@freeuk.com> |
|---|---|
| Date | 2023-01-15 16:27 +0000 |
| Message-ID | <tq19i3$603$1@gioia.aioe.org> |
| In reply to | #88525 |
On 15/01/2023 15:06, Bonita Montero wrote: > Now it's perfect: You said that last time too: On 08/01/2023 05:01, Bonita Montero wrote: > Now it's perfect: > I guess it's more perfect?
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2023-01-15 17:42 +0100 |
| Message-ID | <tq1ac5$2d37g$1@dont-email.me> |
| In reply to | #88528 |
Am 15.01.2023 um 17:27 schrieb Bart: > On 15/01/2023 15:06, Bonita Montero wrote: >> Now it's perfect: > > You said that last time too: > > On 08/01/2023 05:01, Bonita Montero wrote: > > Now it's perfect: > > > > > I guess it's more perfect? The code I've shown yet is also perfect in terms of performance, but not just functionally.
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2023-01-15 17:11 +0000 |
| Message-ID | <tq1c42$2kgfi$1@news.xmission.com> |
| In reply to | #88525 |
In article <tq14n7$2cgbf$1@dont-email.me>, Bonita Montero <Bonita.Montero@gmail.com> wrote: >Now it's perfect: I just need 11,780 votes. Give me a break here. -- If Jeb is Charlie Brown kicking a football-pulled-away, Mitt is a '50s housewife with a black eye who insists to her friends the roast wasn't dry.
[toc] | [prev] | [next] | [standalone]
| From | Tim Woodall <news001@woodall.me.uk> |
|---|---|
| Date | 2023-01-13 06:32 +0000 |
| Message-ID | <tpqtta$d70$1@einstein.home.woodall.me.uk> |
| In reply to | #88434 |
On 2023-01-08, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Now it's perfect:
>
Hmmm, about 20 years ago I was told a story of someone who needed a
'shuffle' function - random permutation. I don't know or don't recall
the context but I'd guess it was part of a test harness.
They wrote it, tested it, deployed it, and things ground to a
standstill.
Instead of permuting, they'd randomly picked numbers, checked for dupes,
and then added to their output, quick for very small sets, 'never'
terminated for large ones.
> while( already.size() != n )
> {
> size_t value;
> do
> value = uid( mt );
> while( already.contains( value ) );
> already.emplace( value );
> cout << value << endl;
> }
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2023-01-13 05:04 -0800 |
| Message-ID | <454ed073-9b47-4e45-a315-909f5cb72f54n@googlegroups.com> |
| In reply to | #88493 |
On Friday, 13 January 2023 at 08:40:21 UTC+2, Tim Woodall wrote:
> On 2023-01-08, Bonita Montero <Bonita....@gmail.com> wrote:
> > Now it's perfect:
> >
> Hmmm, about 20 years ago I was told a story of someone who needed a
> 'shuffle' function - random permutation. I don't know or don't recall
> the context but I'd guess it was part of a test harness.
>
> They wrote it, tested it, deployed it, and things ground to a
> standstill.
>
> Instead of permuting, they'd randomly picked numbers, checked for dupes,
> and then added to their output, quick for very small sets, 'never'
> terminated for large ones.
>
It's bit frustrating to read those stories.
The O(n) algorithm for when the range to pick from and set to be picked
are of close size is not that large or complicated, how they manage
to take tons of time?
size_t n, to, from;
// ...
// assign count to pick to "n" and range to "to" and "from"
// ...
size_t size = to - from + 1;
std::vector<size_t> result(size);
std::mt19937_64 gen(time(nullptr)); // or whatever generator
for(size_t i = 0; i < n; ++i) {
size_t choice = (std::uniform_int_distribution<size_t>( i, size - 1))( gen );
size_t& current = result[i];
size_t& picked = result[choice];
if (current == 0) current = i + 1;
if (picked == 0) picked = choice + 1;
if (current != picked) std::swap(current, picked);
current += from - 1;
// std::cout << current << '\n';
}
result.resize(n);
[toc] | [prev] | [standalone]
Page 4 of 4 — ← Prev page 1 2 3 [4]
Back to top | Article view | comp.lang.c++
csiph-web