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


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

Compute Unique Numbers in a Set

Started byAlbert <invalid@gmail.com>
First post2022-12-26 23:45 +0000
Last post2023-01-15 02:49 +0000
Articles 20 on this page of 92 — 22 participants

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


Contents

  Compute Unique Numbers in a Set Albert <invalid@gmail.com> - 2022-12-26 23:45 +0000
    Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-26 16:52 -0800
      Re: Compute Unique Numbers in a Set tTh <tth@none.invalid> - 2022-12-27 02:44 +0100
        Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-12-27 13:35 -0800
    Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-26 20:47 -0500
      Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-27 15:57 +0000
        Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 11:16 -0500
          Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-27 16:59 +0000
            Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 12:24 -0500
              Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-27 17:53 +0000
                Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 13:50 -0500
                  Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-27 20:08 +0000
                    Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 15:31 -0500
                      Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-28 02:57 +0000
                        Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 23:02 -0500
                          Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-29 02:06 +0000
                            Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-28 23:42 -0500
                              Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-29 12:26 +0000
    Re: Compute Unique Numbers in a Set Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-27 07:34 -0800
    Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-27 16:18 +0000
      Re: Compute Unique Numbers in a Set Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2022-12-28 01:08 +0000
        Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-12-28 03:30 +0000
      Re: Compute Unique Numbers in a Set Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-27 19:42 -0800
    Re: Compute Unique Numbers in a Set Manu Raju <MR@invalid.invalid> - 2022-12-27 18:11 +0000
    Re: Compute Unique Numbers in a Set antispam@math.uni.wroc.pl - 2022-12-28 01:32 +0000
      Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 21:13 -0500
        Re: Compute Unique Numbers in a Set Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-12-27 19:48 -0800
          Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2022-12-27 23:06 -0500
        Re: Compute Unique Numbers in a Set antispam@math.uni.wroc.pl - 2022-12-29 19:47 +0000
    Re: Compute Unique Numbers in a Set jak <nospam@please.ty> - 2022-12-28 11:28 +0100
    Re: Compute Unique Numbers in a Set Rosario19 <Ros@invalid.invalid> - 2023-01-01 21:06 +0100
      Re: Compute Unique Numbers in a Set Rosario19 <Ros@invalid.invalid> - 2023-01-02 06:43 +0100
    Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-01 23:19 +0000
      Re: Compute Unique Numbers in a Set jak <nospam@please.ty> - 2023-01-02 07:28 +0100
        Re: Compute Unique Numbers in a Set jak <nospam@please.ty> - 2023-01-02 08:48 +0100
        Re: Compute Unique Numbers in a Set Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-01-01 23:53 -0800
          Re: Compute Unique Numbers in a Set Öö Tiib <ootiib@hot.ee> - 2023-01-02 01:18 -0800
      Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-02 13:49 +0000
    Re: Compute Unique Numbers in a Set Bart <bc@freeuk.com> - 2023-01-02 12:27 +0000
    Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2023-01-02 12:13 -0500
      Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-02 17:31 +0000
        Re: Compute Unique Numbers in a Set Richard Damon <Richard@Damon-Family.org> - 2023-01-02 12:46 -0500
        Re: Compute Unique Numbers in a Set Siri Cruise <chine.bleu@yahoo.com> - 2023-01-02 18:54 -0800
        Re: Compute Unique Numbers in a Set Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-01-02 20:52 -0800
        Re: Compute Unique Numbers in a Set David Brown <david.brown@hesbynett.no> - 2023-01-03 09:01 +0100
        Re: Compute Unique Numbers in a Set Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-01-03 07:28 -0800
          Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-03 15:46 +0000
            Re: Compute Unique Numbers in a Set David Brown <david.brown@hesbynett.no> - 2023-01-03 18:19 +0100
    Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-08 03:48 +0100
      Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-08 04:18 +0100
      Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-08 03:48 +0000
        Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-08 05:12 +0100
          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 Paavo Helde <eesnimi@osa.pri.ee> - 2023-01-08 21:19 +0200
            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 Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-09 14:48 -0800
                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-09 18:28 -0800
                    Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-09 18:44 -0800
                      Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-10 03:08 +0000
                        Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-09 19:19 -0800
                          Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-10 17:30 +0000
                            Re: Compute Unique Numbers in a Set Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-10 09:45 -0800
                    Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-10 02:57 +0000
                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 Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2023-01-13 03:32 -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 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 tTh <tth@none.invalid> - 2023-01-08 10:13 +0100
            Re: Compute Unique Numbers in a Set Bonita Montero <Bonita.Montero@gmail.com> - 2023-01-08 10:25 +0100
              Re: Compute Unique Numbers in a Set gazelle@shell.xmission.com (Kenny McCormack) - 2023-01-08 13:20 +0000
      Re: Compute Unique Numbers in a Set "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-01-08 13:09 -0800
    Re: Compute Unique Numbers in a Set Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-09 02:37 +0000
    Re: Compute Unique Numbers in a Set John Forkosh <forkosh@panix.com> - 2023-01-15 02:49 +0000

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


#168712

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-01-02 17:31 +0000
Message-ID<87358sq1w7.fsf@bsb.me.uk>
In reply to#168711
Richard Damon <Richard@Damon-Family.org> writes:

> On 1/2/23 10:22 AM, Stefan Ram wrote:
>> Albert <invalid@gmail.com> writes:
>>> Subject: Compute Unique Numbers in a Set
>>    In a set, every number is unique.
>
> Assuming you are using the right Technical definition of Set.

I suspect there's a language problem here.  Without thinking much about
it assumed the result was to be a set (as you know) but I now think the
OP probably meant "compute (i.e choose) unique numbers /from/ a set".
The term "in a set" makes no literal sense and, as noted, clashes with
unique.  But someone might write "in a set" by analogy with mathematical
phrases like, "compute the roots in R".

-- 
Ben.

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


#168713

FromRichard Damon <Richard@Damon-Family.org>
Date2023-01-02 12:46 -0500
Message-ID<YLEsL.27473$cKvc.5678@fx42.iad>
In reply to#168712
On 1/2/23 12:31 PM, Ben Bacarisse wrote:
> Richard Damon <Richard@Damon-Family.org> writes:
> 
>> On 1/2/23 10:22 AM, Stefan Ram wrote:
>>> Albert <invalid@gmail.com> writes:
>>>> Subject: Compute Unique Numbers in a Set
>>>     In a set, every number is unique.
>>
>> Assuming you are using the right Technical definition of Set.
> 
> I suspect there's a language problem here.  Without thinking much about
> it assumed the result was to be a set (as you know) but I now think the
> OP probably meant "compute (i.e choose) unique numbers /from/ a set".
> The term "in a set" makes no literal sense and, as noted, clashes with
> unique.  But someone might write "in a set" by analogy with mathematical
> phrases like, "compute the roots in R".
> 

The problem is some colloquial usages of "Set" are things like, get a 
set of three numbers by rolling 3 six-sided dice.

This "Set" allows for duplicates, and more indicates just a collection, 
and not the normal technical definition of a set being distinct members.

When statement are made in Natural Language, sometimes the difficulty is 
determining which definition is needed to be used for each word.

This sort of imprecision is particularly likely from beginners, which 
the OP sounds to be.

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


#168714

FromSiri Cruise <chine.bleu@yahoo.com>
Date2023-01-02 18:54 -0800
Message-ID<chine.bleu-4FACA3.18543402012023@news.eternal-september.org>
In reply to#168712
In article <87358sq1w7.fsf@bsb.me.uk>,
 Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:

> I suspect there's a language problem here.  Without thinking much about
> it assumed the result was to be a set (as you know) but I now think the
> OP probably meant "compute (i.e choose) unique numbers /from/ a set".
> The term "in a set" makes no literal sense and, as noted, clashes with
> unique.  But someone might write "in a set" by analogy with mathematical
> phrases like, "compute the roots in R".

Taking a wild guess, the answer might be to sort them with useful 
keys. You can then compare any element to all other elements in 
single scan.

-- 
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.    @
'I desire mercy, not sacrifice.'                                    /|\
Discordia: not just a religion but also a parody. This post         / \
I am an Andrea Chen sockpuppet.                  insults Islam.  Mohammed

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


#168715

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2023-01-02 20:52 -0800
Message-ID<86k024p6dp.fsf@linuxsc.com>
In reply to#168712
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Richard Damon <Richard@Damon-Family.org> writes:
>
>> On 1/2/23 10:22 AM, Stefan Ram wrote:
>>
>>> Albert <invalid@gmail.com> writes:
>>>
>>>> Subject:  Compute Unique Numbers in a Set
>>>
>>>    In a set, every number is unique.

It seems likely that OP was speaking informally.  Probably he
meant some sort of generic collection rather than specifically a
mathematical set.

>> Assuming you are using the right Technical definition of Set.
>
> I suspect there's a language problem here.  Without thinking much about
> it assumed the result was to be a set (as you know) but I now think the
> OP probably meant "compute (i.e choose) unique numbers /from/ a set".
> The term "in a set" makes no literal sense and, as noted, clashes with
> unique.  But someone might write "in a set" by analogy with mathematical
> phrases like, "compute the roots in R".

The original posting said

      Is this the best way to generate unique random numbers
      in a set of 6 numbers?

which reinforces my view that OP meant something like collection
rather than something having to do with set as the term is used
in mathematics.

I confess I paid more attention to the code than to how the
question was asked.  As a result of that, the comments I gave
assumed that the ordering of the six numbers was relevant.
However, looking back on it now, if I had to bet my bet would
be that OP doesn't care about the order, but just wants six
unique numbers (chosen from the relevant range).

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


#168716

FromDavid Brown <david.brown@hesbynett.no>
Date2023-01-03 09:01 +0100
Message-ID<tp0ndc$231d4$1@dont-email.me>
In reply to#168712
On 02/01/2023 18:31, Ben Bacarisse wrote:
> Richard Damon <Richard@Damon-Family.org> writes:
> 
>> On 1/2/23 10:22 AM, Stefan Ram wrote:
>>> Albert <invalid@gmail.com> writes:
>>>> Subject: Compute Unique Numbers in a Set
>>>     In a set, every number is unique.
>>
>> Assuming you are using the right Technical definition of Set.
> 
> I suspect there's a language problem here.  Without thinking much about
> it assumed the result was to be a set (as you know) but I now think the
> OP probably meant "compute (i.e choose) unique numbers /from/ a set".
> The term "in a set" makes no literal sense and, as noted, clashes with
> unique.  But someone might write "in a set" by analogy with mathematical
> phrases like, "compute the roots in R".
> 

Given that the OP has apparently completely ignored all the help and 
advice given here, and seems to have no intention of clarifying or 
expounding upon the first post, I think it is pointless to speculate on 
what the OP might have meant.  Just take the question as inspiration - 
interpret it any way that can lead to interesting code, algorithms or 
discussions.  (This is what has been going on so far in this thread.)

When the OP comes back with "Thanks for the help so far, but what I 
really meant was ... ", then it is possible to give focused help.

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


#168717

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2023-01-03 07:28 -0800
Message-ID<86fscrprhd.fsf@linuxsc.com>
In reply to#168712
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Richard Damon <Richard@Damon-Family.org> writes:
[...]

>> [...]
>
> [...]

P.S.  I want to add, I liked seeing the various alternative
approaches that were given, both by you and by Richard Damon
(and also by others).  Some of them were new to me.  In a way
it's good that there were language difficulties, as otherwise
these other approaches may not have come up.

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


#168718

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2023-01-03 15:46 +0000
Message-ID<tp1ikf$241lj$1@news.xmission.com>
In reply to#168717
In article <86fscrprhd.fsf@linuxsc.com>,
Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
>Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> Richard Damon <Richard@Damon-Family.org> writes:
>[...]
>
>>> [...]
>>
>> [...]
>
>P.S.  I want to add, I liked seeing the various alternative
>approaches that were given, both by you and by Richard Damon
>(and also by others).  Some of them were new to me.  In a way
>it's good that there were language difficulties, as otherwise
>these other approaches may not have come up.

You have to consider the possibility that posts such as the OP here are
machine-generated.  And that's not necessarily a bad thing.  It gets
discussion going.  It breathes life into an otherwise almost-dead medium
(Usenet).

I know of one "social media" site that openly has bot-written questions.
They are often silly/stupid, but they do keep discussion going.

-- 
If you don't have faith, it's because you are reading the Bible with an
honest, truthful, real-answer seeking heart.

    - Rick C Hodgin -

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


#168719

FromDavid Brown <david.brown@hesbynett.no>
Date2023-01-03 18:19 +0100
Message-ID<tp1o2f$26738$1@dont-email.me>
In reply to#168718
On 03/01/2023 16:46, Kenny McCormack wrote:
> In article <86fscrprhd.fsf@linuxsc.com>,
> Tim Rentsch  <tr.17687@z991.linuxsc.com> wrote:
>> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>
>>> Richard Damon <Richard@Damon-Family.org> writes:
>> [...]
>>
>>>> [...]
>>>
>>> [...]
>>
>> P.S.  I want to add, I liked seeing the various alternative
>> approaches that were given, both by you and by Richard Damon
>> (and also by others).  Some of them were new to me.  In a way
>> it's good that there were language difficulties, as otherwise
>> these other approaches may not have come up.
> 
> You have to consider the possibility that posts such as the OP here are
> machine-generated.  And that's not necessarily a bad thing.  It gets
> discussion going.  It breathes life into an otherwise almost-dead medium
> (Usenet).
> 
> I know of one "social media" site that openly has bot-written questions.
> They are often silly/stupid, but they do keep discussion going.
> 

I think the initial post would be fairly advanced for a 
machine-generated post.  That means significant time and effort - which 
means significant money.  And the question then is, why?

A technical Usenet group might have around 50 regular members, several 
hundred lurkers who are mostly read-only, and be google-groups searched 
by a few thousand.  There is no advertising, and thus no income.  So 
there is nothing to be gained by making such machine-generated posts in 
Usenet.

Compare that to insta-snapo-book social media, with millions of users, 
valuable advertising, ranking and "liking" systems, and a far more 
gullible readership.  Some areas of these social medias are dominated by 
bots making posts and liking each others posts to modify rankings and 
generate artificial advertising income, or to push their political 
agendas or crazy conspiracy theories, perhaps even as an attack on 
another country.  It is a different world entirely.


You get the odd vandal that uses automated software to make Usenet 
posts, but these are mostly absurd posts.  And there are a few cases 
where people have made Usenet poster programs for fun, to imitate 
particularly annoying posters.

The original post is much more likely to be homework than a bot of any kind.


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


#168750

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-08 03:48 +0100
Message-ID<tpdas0$3oa34$1@dont-email.me>
In reply to#168644
What about this:

#include <iostream>
#include <array>
#include <random>

using namespace std;

int main()
{
	constexpr size_t MAX = 6;
	array<int, MAX> already;
	mt19937_64 mt;
	uniform_int_distribution<int> uid( 1, 60 );
	auto base = already.cbegin();
	for( size_t n = 0; n != MAX; )
	{
		int value;
		while( find( base, base + n, value = uid( mt ) ) != base + n );
		cout << (already[n++] = value) << endl;
	}
}

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


#168751

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-08 04:18 +0100
Message-ID<tpdcla$3oi34$1@dont-email.me>
In reply to#168750
More readable:

#include <iostream>
#include <array>
#include <random>

using namespace std;

int main()
{
	constexpr size_t MAX = 6;
	array<int, MAX> already;
	mt19937_64 mt;
	uniform_int_distribution<int> uid( 1, 60 );
	for( auto begin = already.begin(), end = begin; end != already.end(); )
	{
		int value;
		do
			value = uid( mt );
		while( find( begin, end, value ) != end );
		*end++ = value;
		cout << value << endl;
	}
}

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


#168752

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2023-01-08 03:48 +0000
Message-ID<tpdef6$2a759$1@news.xmission.com>
In reply to#168750
In article <tpdas0$3oa34$1@dont-email.me>,
Bonita Montero  <Bonita.Montero@gmail.com> wrote:
>What about this:
>
>#include <iostream>
>#include <array>
>#include <random>
>
>using namespace std;

Off topic.

-- 
He must be a Muslim. He's got three wives and he doesn't drink.

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


#168753

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-08 05:12 +0100
Message-ID<tpdfp6$3osch$1@dont-email.me>
In reply to#168752
Now with maximum-flexibility:

#include <iostream>
#include <unordered_set>
#include <charconv>
#include <random>
#include <concepts>

using namespace std;

int main( int argc, char **argv )
{
	try
	{
		if( argc < 4 )
		{
			cout << argv[0] << " n from to" << endl;
			return EXIT_FAILURE;
		}
		auto parse = []( char const *str, size_t &value, char const *err )
		{
			if( from_chars_result fcr = from_chars( str, str + strlen( str ), 
value ); (bool)fcr.ec || *fcr.ptr )
				throw invalid_argument( err );
		};
		size_t n, from, to;
		parse( argv[1], n, "wrong number of values" );
		parse( argv[2], from, "wrong from-value" );
		parse( argv[3], to, "wrong to-value" );
		if( from > to )
			swap( from, to );
		unordered_set<size_t> already;
		already.reserve( n );
		mt19937_64 mt;
		uniform_int_distribution<size_t> uid( from, to );
		for( size_t i = 0; i != n; ++i )
		{
			if( already.size() == to - from + 1 )
			{
				cout << "values exhausted" << endl;
				return EXIT_FAILURE;
			}
			size_t value;
			do
				value = uid( mt );
			while( already.find( value ) != already.end() );
			already.emplace( value );
			cout << value << endl;
		}
	}
	catch( exception const &exc )
	{
		cout << exc.what() << endl;
		return EXIT_FAILURE;
	}
}

This is not off-topic since the program does the same.

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


#168754

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-08 06:01 +0100
Message-ID<tpdim3$3p3kb$1@dont-email.me>
In reply to#168753
Now it's perfect:

#include <iostream>
#include <unordered_set>
#include <charconv>
#include <random>

using namespace std;

int main( int argc, char **argv )
{
	try
	{
		if( argc < 4 )
			return
				cout << argv[0] << " n from to" << endl,
				EXIT_FAILURE;
		auto parse = []( char const *str, char const *err )
		{
			size_t value;
			if( from_chars_result fcr = from_chars( str, str + strlen( str ), 
value ); (bool)fcr.ec || *fcr.ptr )
				throw invalid_argument( err );
			return value;
		};
		size_t
			n = parse( argv[1], "wrong number of values" ),
			from = parse( argv[2], "wrong from-value" ),
			to = parse( argv[3], "wrong to-value" );
		if( from > to )
			swap( from, to );
		if( !n || n - 1 > to - from )
			return
				cout << "n is too small" << endl,
				EXIT_FAILURE;
		unordered_set<size_t> already;
		already.reserve( n );
		mt19937_64 mt;
		uniform_int_distribution<size_t> uid( from, to );
		while( already.size() != n )
		{
			size_t value;
			do
				value = uid( mt );
			while( already.contains( value ) );
			already.emplace( value );
			cout << value << endl;
		}
	}
	catch( exception const &exc )
	{
		return
			cout << exc.what() << endl,
			EXIT_FAILURE;
	}
}

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


#168762

FromBart <bc@freeuk.com>
Date2023-01-08 14:48 +0000
Message-ID<tpel4n$ivd$1@gioia.aioe.org>
In reply to#168754
On 08/01/2023 05:01, Bonita Montero wrote:
> Now it's perfect:
> 
> #include <iostream>
> #include <unordered_set>
> #include <charconv>
> #include <random>
> 
> using namespace std;
> 
> int main( int argc, char **argv )
> {
>      try
>      {
>          if( argc < 4 )
>              return
>                  cout << argv[0] << " n from to" << endl,
>                  EXIT_FAILURE;
>          auto parse = []( char const *str, char const *err )
>          {
>              size_t value;
>              if( from_chars_result fcr = from_chars( str, str + strlen( 
> str ), value ); (bool)fcr.ec || *fcr.ptr )
>                  throw invalid_argument( err );
>              return value;
>          };
>          size_t
>              n = parse( argv[1], "wrong number of values" ),
>              from = parse( argv[2], "wrong from-value" ),
>              to = parse( argv[3], "wrong to-value" );
>          if( from > to )
>              swap( from, to );
>          if( !n || n - 1 > to - from )
>              return
>                  cout << "n is too small" << endl,
>                  EXIT_FAILURE;
>          unordered_set<size_t> already;
>          already.reserve( n );
>          mt19937_64 mt;
>          uniform_int_distribution<size_t> uid( from, to );
>          while( already.size() != n )
>          {
>              size_t value;
>              do
>                  value = uid( mt );
>              while( already.contains( value ) );
>              already.emplace( value );
>              cout << value << endl;
>          }
>      }
>      catch( exception const &exc )
>      {
>          return
>              cout << exc.what() << endl,
>              EXIT_FAILURE;
>      }
> }


That's some torturous-looking code. My attempt for the same spec is give 
below, in scripting code. Despite that, I'd argue that it would simpler 
to use that as a starting point to create a C version rather than try 
and adapt your C++ code, because it is basically pseudo-code.

Half of this is just reading and checking command line inputs which can 
be in any manner at all. In any case it's of little relevance; of much 
more use would be a function that returned a set of N unique random 
numbers, that can be embedded into another program.

Note that your error message is wrong: N would be too large, not too small.

Neither version seeds the PRNG so they're both useless on repeated runs.

----------------------------------------------------------------

     if ncmdparams<>3 then
         println "Usage: N From To"
         stop 1
     fi

     read n:"i", lower:"i", upper:"i"

     if lower>upper then swap(lower,upper) fi

     if n > upper-lower+1 then
         abort("N is too large")
     fi

     a::=()

     to n do
         repeat
             x:=random(lower..upper)
         until x not in a
         a &:= x
         println x
     od

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


#168763

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-08 18:22 +0100
Message-ID<tpeu32$3t2it$1@dont-email.me>
In reply to#168762
Am 08.01.2023 um 15:48 schrieb Bart:

> That's some torturous-looking code. My attempt for the same spec is give 
> below, in scripting code. Despite that, I'd argue that it would simpler 
> to use that as a starting point to create a C version rather than try 
> and adapt your C++ code, because it is basically pseudo-code.

C++ is used when things are usually not that simple and you have a
performance requirement. If you strip the screen output and run the
code with a large number of values any the given C++ code is magni-
tudes faster than any scripting code.

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


#168765

FromBart <bc@freeuk.com>
Date2023-01-08 17:46 +0000
Message-ID<tpevhg$1g8i$1@gioia.aioe.org>
In reply to#168763
On 08/01/2023 17:22, Bonita Montero wrote:
> Am 08.01.2023 um 15:48 schrieb Bart:
> 
>> That's some torturous-looking code. My attempt for the same spec is 
>> give below, in scripting code. Despite that, I'd argue that it would 
>> simpler to use that as a starting point to create a C version rather 
>> than try and adapt your C++ code, because it is basically pseudo-code.
> 
> C++ is used when things are usually not that simple and you have a
> performance requirement. If you strip the screen output and run the
> code with a large number of values any the given C++ code is magni-
> tudes faster than any scripting code.
> 

I posted my last reply before I even saw this comment. I explained how I 
did exactly that! My script was faster.

I've now tried N=10M and results are:

C++                    79   seconds  (g++ -O0)
C++                    16   seconds  (g++ -O3)
Script (1)             13   seconds  (HLL only, unoptimised)
Script (2)              9.5 seconds  (ASM-accelerated unoptimised)
Script (3)              7.3 seconds  (HLL only, optimised via C/gcc/-O3)

The 'optimisation' refers to the interpreter; the program is still bytecode.

The interesting thing is that my unoptimised interpeter, executing 
byte-code, is 6 times as fast as unoptimised C++ executing native code.

(However I don't know what's going on inside your C++ libraries; this is 
where C has the advantage of transparency.)

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


#168773

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-09 04:58 +0100
Message-ID<tpg3bi$3bab$1@dont-email.me>
In reply to#168765
Am 08.01.2023 um 18:46 schrieb Bart:

> I've now tried N=10M and results are:
> 
> C++                    79   seconds  (g++ -O0)
> C++                    16   seconds  (g++ -O3)
> Script (1)             13   seconds  (HLL only, unoptimised)
> Script (2)              9.5 seconds  (ASM-accelerated unoptimised)
> Script (3)              7.3 seconds  (HLL only, optimised via C/gcc/-O3)

Whatever you tested: you dindn't test my code.

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


#168775

FromBart <bc@freeuk.com>
Date2023-01-09 11:26 +0000
Message-ID<tpgtlv$18cp$1@gioia.aioe.org>
In reply to#168773
On 09/01/2023 03:58, Bonita Montero wrote:
> Am 08.01.2023 um 18:46 schrieb Bart:
> 
>> I've now tried N=10M and results are:
>>
>> C++                    79   seconds  (g++ -O0)
>> C++                    16   seconds  (g++ -O3)
>> Script (1)             13   seconds  (HLL only, unoptimised)
>> Script (2)              9.5 seconds  (ASM-accelerated unoptimised)
>> Script (3)              7.3 seconds  (HLL only, optimised via C/gcc/-O3)
> 
> Whatever you tested: you dindn't test my code.
> 
> 
It wasn't /my/ code because I don't know C++. It was just tweaked from 
yours:

#include <iostream>
#include <unordered_set>
#include <charconv>
#include <random>
#include <string.h>

using namespace std;

int main( int argc, char **argv )
{
     try
     {
	int n=10000000, from=1, to=n+1;
         unordered_set<size_t> already;
         already.reserve( n );
         mt19937_64 mt;
         uniform_int_distribution<size_t> uid( from, to );
long long int sum=0;
int i=0;
         while( already.size() != n )
         {
             size_t value;
             do
                 value = uid( mt );
             while( already.contains( value ) );
             already.emplace( value );
             sum+=value;
//            cout << value << " " << sum << " " << ++i << endl;
         }
             cout << sum << endl;

     }
     catch( exception const &exc )
     {
         return
             cout << exc.what() << endl,
             EXIT_FAILURE;
     }
}


(The calculated 'sum' is not the sum of all the final set of values; I 
realised I had no idea how to access those in a separate loop.)

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


#168778

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-09 15:57 +0100
Message-ID<tph9ve$7i53$1@dont-email.me>
In reply to#168775
Am 09.01.2023 um 12:26 schrieb Bart:
> On 09/01/2023 03:58, Bonita Montero wrote:
>> Am 08.01.2023 um 18:46 schrieb Bart:
>>
>>> I've now tried N=10M and results are:
>>>
>>> C++                    79   seconds  (g++ -O0)
>>> C++                    16   seconds  (g++ -O3)
>>> Script (1)             13   seconds  (HLL only, unoptimised)
>>> Script (2)              9.5 seconds  (ASM-accelerated unoptimised)
>>> Script (3)              7.3 seconds  (HLL only, optimised via C/gcc/-O3)
>>
>> Whatever you tested: you dindn't test my code.
>>
>>
> It wasn't /my/ code because I don't know C++. It was just tweaked from 
> yours:
> 
> #include <iostream>
> #include <unordered_set>
> #include <charconv>
> #include <random>
> #include <string.h>
> 
> using namespace std;
> 
> int main( int argc, char **argv )
> {
>      try
>      {
>      int n=10000000, from=1, to=n+1;
>          unordered_set<size_t> already;
>          already.reserve( n );
>          mt19937_64 mt;
>          uniform_int_distribution<size_t> uid( from, to );
> long long int sum=0;
> int i=0;
>          while( already.size() != n )
>          {
>              size_t value;
>              do
>                  value = uid( mt );
>              while( already.contains( value ) );
>              already.emplace( value );
>              sum+=value;
> //            cout << value << " " << sum << " " << ++i << endl;
>          }
>              cout << sum << endl;
> 
>      }
>      catch( exception const &exc )
>      {
>          return
>              cout << exc.what() << endl,
>              EXIT_FAILURE;
>      }
> }
> 
> 
> (The calculated 'sum' is not the sum of all the final set of values; I 
> realised I had no idea how to access those in a separate loop.)

You must have made sth. wrong.

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


#168764

FromBart <bc@freeuk.com>
Date2023-01-08 17:34 +0000
Message-ID<tpeurf$15ot$1@gioia.aioe.org>
In reply to#168762
On 08/01/2023 14:48, Bart wrote:
> On 08/01/2023 05:01, Bonita Montero wrote:
>> Now it's perfect:
>>
>> <snip complicated C++ code>>

> That's some torturous-looking code.

I tested your [BM] code for speed, using N=1000000, and limits of 1..N+1.

The 'cout' in the loop was replaced with a count of all the values, so 
that at the end the total was displayed to be able to compare results.

The C++ with gcc-O3 took 1.25 seconds.

My script initially took much longer due to using an unordered list 
(storing up to 1M values). But I tweaked it use a bit-set.

Then it took 0.67 seconds.


(Revised script:)
>      a:=new(set,lower..upper)
>      sum:=0

>      to n do
>          repeat
>              x:=random(lower..upper)
>          until x not in a
>          a[x]:=1
 >          sum+:=x
>      od

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


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

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


csiph-web