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 12 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 5 of 5 — ← Prev page 1 2 3 4 [5]


#168798

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-13 15:02 +0100
Message-ID<tpro7o$1ki63$1@dont-email.me>
In reply to#168797
Am 13.01.2023 um 14:55 schrieb Kenny McCormack:
> In article <tprl89$1k839$1@dont-email.me>,
> some lunatic  <Bonita.Montero@gmail.com> wrote:
>> Am 13.01.2023 um 13:21 schrieb Kenny McCormack:
>>> In article <tpr12i$1i7r6$1@dont-email.me>,
>>> some lunatic  <Bonita.Montero@gmail.com> wrote:
>>> ...
>>>> Now I make a list of eligible numbers, randomly chode one of them
>>>> and remove the item from the list:
>>>>
>>>> #include <iostream>
>>>> #include <charconv>
>>>> #include <random>
>>>> #include <concepts>
>>>> #include <vector>
>>>
>>> You still just don't seem to get this whole topicality thing, do ya?
>>
>> My code is beyond that topic. You may elect only a small number
>> of values like in the original code, but you might also chose
>> million of numbers.
> 
> Whoooooosh!!!

It's not the absolute number of values which can be chosen
but the flexibility or finding the most efficient way to
have this flexibility.

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


#168799

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2023-01-13 14:17 +0000
Message-ID<tprp68$2hb2h$2@news.xmission.com>
In reply to#168798
In article <tpro7o$1ki63$1@dont-email.me>,
some lunatic  <Bonita.Montero@gmail.com> wrote:
>Am 13.01.2023 um 14:55 schrieb Kenny McCormack:
>> In article <tprl89$1k839$1@dont-email.me>,
>> some lunatic  <Bonita.Montero@gmail.com> wrote:
>>> Am 13.01.2023 um 13:21 schrieb Kenny McCormack:
>>>> In article <tpr12i$1i7r6$1@dont-email.me>,
>>>> some lunatic  <Bonita.Montero@gmail.com> wrote:
>>>> ...
>>>>> Now I make a list of eligible numbers, randomly chode one of them
>>>>> and remove the item from the list:
>>>>>
>>>>> #include <iostream>
>>>>> #include <charconv>
>>>>> #include <random>
>>>>> #include <concepts>
>>>>> #include <vector>
>>>>
>>>> You still just don't seem to get this whole topicality thing, do ya?
>>>
>>> My code is beyond that topic. You may elect only a small number
>>> of values like in the original code, but you might also chose
>>> million of numbers.
>> 
>> Whoooooosh!!!
>
>It's not the absolute number of values which can be chosen
>but the flexibility or finding the most efficient way to
>have this flexibility.

Due anziani che convivono da una vita:
Lei: "Caro, ormai ci potremmo anche sposare...".
Lui: "Ma cara, alla nostra eta', chi ci prenderebbe?".

-- 
"Women should not be enlightened or educated in any way. They should be
segregated because they are the cause of unholy erections in holy men.

    -- Saint Augustine (354-430) --

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


#168814

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-15 16:06 +0100
Message-ID<tq14n7$2cgbf$1@dont-email.me>
In reply to#168793
Now it's perfect:

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

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" );
		if( !n )
			return EXIT_SUCCESS;
		size_t
			from = parse( argv[2], "wrong from-value" ),
			to = parse( argv[3], "wrong to-value" );
		if( from > to )
			swap(from, to);
		if( n - 1 > to - from )
			return
				cout << "n is too small" << endl,
				EXIT_FAILURE;
		unordered_set<size_t> values;
		mt19937_64 mt;
		uniform_int_distribution<size_t> uid( from, to );
		if( to - from != (size_t)-1 && to - from + 1 < n / 2 )
		{
			values.reserve( n );
			while( values.size() < n )
			{
				size_t value;
				do
					value = uid( mt );
				while( values.contains( value ) );
				values.emplace( value );
			}
		}
		else
		{
			if( to - from == (size_t)-1 )
				throw bad_alloc();
			values.reserve( to - from + 1 );
			size_t i = from - 1;
			do
				values.emplace( ++i );
			while( i != to );
			while( values.size() > n )
				for( ; ; )
				{
					auto itRemove = values.find( uid( mt ) );
					if( itRemove == values.end() )
						continue;
					values.erase( itRemove );
					break;
				}
		}
		for( size_t value : values )
			cout << value << endl;
	}
	catch( exception const &exc )
	{
		return
			cout << exc.what() << endl,
			EXIT_FAILURE;
	}
}

If less than half of the range of numbers is selected (n < (to - from
+ 1) / 2) the unordered map is filled up to n elements and if an element
is found that's already there another value is chosen. If more than half
of the range is selected (n >= (to - from + 1) / 2) the set is filled up
to contain the whole range and the elements are incrementally removed at
a random value.

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


#168815

FromBart <bc@freeuk.com>
Date2023-01-15 16:27 +0000
Message-ID<tq19i3$603$1@gioia.aioe.org>
In reply to#168814
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]


#168816

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-15 17:42 +0100
Message-ID<tq1ac5$2d37g$1@dont-email.me>
In reply to#168815
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]


#168817

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2023-01-15 17:11 +0000
Message-ID<tq1c42$2kgfi$1@news.xmission.com>
In reply to#168814
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]


#168756

FromtTh <tth@none.invalid>
Date2023-01-08 10:13 +0100
Message-ID<tpe1f8$bo4$1@news.gegeweb.eu>
In reply to#168753
On 1/8/23 05:12, Bonita Montero wrote:
> Now with maximum-flexibility:

    [snip++]

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

    So you can rewrite it in ALGOL68.

-- 
+-------------------------------------------------------------------+
| https://diaspora.psyco.fr/people/2516d770f3740139d80a001e67d879df |
|                                 https://danstonchat.com/1138.html |
+-------------------------------------------------------------------+

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


#168757

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-01-08 10:25 +0100
Message-ID<tpe252$3qdq5$1@dont-email.me>
In reply to#168756
Am 08.01.2023 um 10:13 schrieb tTh:

> On 1/8/23 05:12, Bonita Montero wrote:

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

>     So you can rewrite it in ALGOL68.

Maybe albert should learn C++ instead of C and he would save much work.
And 95% what you can do in C is the same in C++.

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


#168759

Fromgazelle@shell.xmission.com (Kenny McCormack)
Date2023-01-08 13:20 +0000
Message-ID<tpefu9$2am0l$1@news.xmission.com>
In reply to#168757
In article <tpe252$3qdq5$1@dont-email.me>,
Bonita Montero  <Bonita.Montero@gmail.com> wrote:
>Am 08.01.2023 um 10:13 schrieb tTh:
>
>> On 1/8/23 05:12, Bonita Montero wrote:
>
>>> This is not off-topic since the program does the same.
>
>>   So you can rewrite it in ALGOL68.
>
>Maybe albert should learn C++ instead of C and he would save much work.
>And 95% what you can do in C is the same in C++.
>

Wouldn't it be great if there actually was a Usenet newsgroup dedicated to C++?

But I guess that until that happy day arrives, we'll have to just post our
C++ programs here.  Such is life...

-- 

First of all, I do not appreciate your playing stupid here at all.

	- Thomas 'PointedEars' Lahn -

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


#168768

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-01-08 13:09 -0800
Message-ID<tpfbeu$3u76h$4@dont-email.me>
In reply to#168750
On 1/7/2023 6:48 PM, Bonita Montero wrote:
> 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;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If anybody accidentally drinks poison: Read the line of code above. It 
will make you puke. Yikes!



>      }
> }

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


#168772

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-01-09 02:37 +0000
Message-ID<871qo4whzz.fsf@bsb.me.uk>
In reply to#168644
Albert <invalid@gmail.com> writes:

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

No one has mentioned this algorithm that can do the job with just one
random number:

  To choose k numbers from [1..n]:
  Pick a number between 0 and combinations(n, k) - 1.
  Turn that number (the index of the chosen combination in the sorted
  list of all possible combinations) into the combination itself.

The numbers should not be too big (6 from 60 is fine) and you need a
generator with enough bits.  64 or even just a random double in [0,1)
will do for this particular application.

typedef unsigned long long Nat;

Nat combinations(int n, int k)
{
     Nat r = 1;
     unsigned i = 1;
     while (i <= k) r = r * n-- / i++;
     return r;
}

void index_to_combination(Nat r, int n, int k, int *comb)
{
     int x = 1, i = 0;
     while (i < k) {
          Nat c;
          while (r >= (c = combinations(n - x, k - i - 1)))
               r -= c, x++;
          comb[i++] = x++;
     }
}

This is a case where I don't think using other names will help.  The way
to make this code understandable is to write a good comment that
describes the algorithm.

-- 
Ben.

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


#168812

FromJohn Forkosh <forkosh@panix.com>
Date2023-01-15 02:49 +0000
Message-ID<tpvpke$ssh$1@reader2.panix.com>
In reply to#168644
Albert <invalid@gmail.com> wrote:
> Is this the best way to generate unique random numbers in a set of 6 
> numbers? <snip>

I think what you're looking for (not sure whether or not it's already
been mentioned in this long thread) is a "partial permutation",
swapping used numbers with unused ones, but we don't need to save the
used ones. Here's my general solution, and cc -DTESTDRIVE for the
main() test driver...

/* ---
 * uniqrand.c
 * Generate nuniq random numbers in the range lorand...hirand
 * ---
 * (optional) command-line arguments:
 *    ./uniqrand  nuniq  lorand  hirand    seedval
 *                  100       1     999  864297531  <-- default test values
 * ------------------------------------------------------------------------ */

/* --- standard headers --- */
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h> /* for MIN(i,j) macro */

/* --- standard headers --- */
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h> /* for MIN(i,j) macro */

/* --- entry point --- */
int *uniqrand ( int nuniq, int lorand, int hirand ) {
  /* --- allocations and declarations --- */
  int  iuniq = 0;                       /* index 0...nuniq-1 */
  int  range = 1+abs(hirand-lorand);    /* random number range */
  int  *randvals  = NULL,               /* nuniq ints returned to caller */
       *rangevals = NULL;               /* all numbers lorand...hirand */
  /* --- initialization --- */
  if ( nuniq<1 || nuniq>range ) goto end_of_job; /* input error */  
  randvals  = (int *)malloc((nuniq+99)*sizeof(int)); /* caller free()'s */
  rangevals = (int *)malloc((range+99)*sizeof(int)); /* we free() work area */
  if ( randvals==NULL || rangevals==NULL ) goto end_of_job; /* malloc error */
  rangevals[0] = MIN(lorand,hirand);    /* in case hirand<lorand */
  for ( iuniq=1; iuniq<range; iuniq++ ) /* all numbers lorand...hirand... */
    rangevals[iuniq] = rangevals[0] + iuniq; /* ...or vice versa */
  /* --- generate nuniq unique random numbers from lorand to hirand --- */
  for ( iuniq=0; iuniq<nuniq; iuniq++ ) {
    int  thisrand = rand()%range;       /* random number 0...range-1 */
    randvals[iuniq] = rangevals[thisrand]; /* unique random lorand...hirand */
    rangevals[thisrand] = rangevals[--range]; } /* replace used number */
  /* --- end-of-job --- */
  end_of_job:
    if ( rangevals != NULL ) free((void *)rangevals);
    return ( randvals );
  } /* --- end-of-function uniqrand() --- */

#if defined(TESTDRIVE)
/* --- entry point --- */
int main ( int argc, char *argv[] ) {
  /* --- command-line arguments --- */
  int    nuniq = ( argc>1? atoi(argv[1]) :       100 ),
        lorand = ( argc>2? atoi(argv[2]) :         1 ),
        hirand = ( argc>3? atoi(argv[3]) :       999 ),
       seedval = ( argc>4? atoi(argv[4]) : 164897532 );
  /* --- allocations and declarations --- */
  int *uniqrand(), *randvals=NULL;      /* randvals from uniqrand() */
  int iuniq = 0;                        /* randvals[] index */
  /* --- initialization --- */
  if ( seedval > 0 ) srand(seedval);    /* initialize rand() */
  /* --- call uniqrand() and display results --- */
  if ( (randvals=uniqrand(nuniq,lorand,hirand)) == NULL ) goto end_of_job;
  for ( iuniq=0; iuniq<nuniq; iuniq++ )
    printf("%7d%s",randvals[iuniq],
           (iuniq==nuniq-1?"\n":((iuniq+1)%8==0?",\n":",")) );
  /* --- end-of-job --- */
  end_of_job:
    if ( randvals != NULL ) free((void *)randvals);
    exit ( 0 );
  } /* --- end-of-function main() --- */
#endif
/* --- end-of-file uniqrand.c --- */

John Forkosh  ( mailto:  j@f.com  where j=john and f=forkosh )

[toc] | [prev] | [standalone]


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

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


csiph-web