Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Conquest is easy. Control is not. -- Kirk, "Mirror, Mirror", stardate unknown


devel / comp.lang.c / Looping through 0..3 randomly

SubjectAuthor
* Looping through 0..3 randomlyOğuz
+* Re: Looping through 0..3 randomlyKaz Kylheku
|`* Re: Looping through 0..3 randomlyOğuz
| `- Re: Looping through 0..3 randomlyBen Bacarisse
+- Re: Looping through 0..3 randomlyBonita Montero
`- Re: Looping through 0..3 randomlyThiago Adams

1
Looping through 0..3 randomly

<a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19260&group=comp.lang.c#19260

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4495:: with SMTP id x21mr34181767qkp.633.1638257952128;
Mon, 29 Nov 2021 23:39:12 -0800 (PST)
X-Received: by 2002:a05:622a:1014:: with SMTP id d20mr50382512qte.399.1638257951944;
Mon, 29 Nov 2021 23:39:11 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 29 Nov 2021 23:39:11 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=88.224.106.70; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 88.224.106.70
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
Subject: Looping through 0..3 randomly
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Tue, 30 Nov 2021 07:39:12 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 10
 by: Oğuz - Tue, 30 Nov 2021 07:39 UTC

This is how I do it now:

int a[] = { 0, 1, 2, 3 };
shuffle(a, 4);
for (int i = 0; i < 4; i++) {
// do something with `a[i]'
}

where `shuffle' is a function that I copied from here: https://benpfaff.org/writings/clc/shuffle.html

It works fine, but since I have only four numbers to loop over and they are consequential, putting them into an array and shuffling it feels a bit overkill. Is there an easier way to do this (without using an array, preferably)?

Re: Looping through 0..3 randomly

<20211130000937.247@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19261&group=comp.lang.c#19261

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Looping through 0..3 randomly
Date: Tue, 30 Nov 2021 08:32:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <20211130000937.247@kylheku.com>
References: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 30 Nov 2021 08:32:42 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="9ee16b5229fb530a47dcfcff595d5844";
logging-data="29587"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MJj17X7YbMW5ounBF0vTKg8nJE8tkyOM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:13ChEdMgTNyvLN3omKm25h9ZWr8=
 by: Kaz Kylheku - Tue, 30 Nov 2021 08:32 UTC

On 2021-11-30, Oğuz <oguzismailuysal@gmail.com> wrote:
> This is how I do it now:
>
> int a[] = { 0, 1, 2, 3 };
> shuffle(a, 4);
> for (int i = 0; i < 4; i++) {
> // do something with `a[i]'
> }
>
> where `shuffle' is a function that I copied from here: https://benpfaff.org/writings/clc/shuffle.html
>
> It works fine, but since I have only four numbers to loop over and
> they are consequential, putting them into an array and shuffling it
> feels a bit overkill. Is there an easier way to do this (without using
> an array, preferably)?

There are 24 permutations; so for instance you could make a program that
uses a cascade of randomized selection statements to take one of 24
control flow pathways. That's not going to be compact compared to the
shuffled array.

The values 0-3 require only two bits, and so a permutation can fit into
a byte. In 24 bytes of space, you could save all 24 permutations,
and randomly select one, then extract the values.

What are the values:

4> (mapcar (op reduce-left (op + @2 (* @1 4))) (perm '(0 1 2 3) 4))
(27 30 39 45 54 57 75 78 99 108 114 120 135 141 147 156 177 180
198 201 210 216 225 228)

So we still have an array, but it's immutable; we aren't having to
shuffle it. Moreover, a single call to the PRNG gets us the permutation,
and we are done with the array after making one access to it:

static const unsigned char tab[24] = {
27, 30, 39, 45, 54, 57, 75, 78,
99, 108, 114, 120, 135, 141, 147, 156,
177, 180, 198, 201, 210, 216, 225, 228
};

int i;
unsigned char perm = tab[rand() % 24]; /* get a better PRNG */

/* do something with (perm >> 6) */
/* do something with (perm >> 4) & 3 */
/* do something with (perm >> 2) & 3 */
/* do something with perm & 3 */

/* or */

for (int i = 0; i < 4; i++) {
/* do something with perm & 3 */
perm >>= 2;
}

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: Looping through 0..3 randomly

<a2b20b48-4227-4d36-b0bb-bee3d45a9185n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19262&group=comp.lang.c#19262

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1cd:: with SMTP id t13mr41828605qtw.487.1638271840069;
Tue, 30 Nov 2021 03:30:40 -0800 (PST)
X-Received: by 2002:a05:620a:440d:: with SMTP id v13mr35323427qkp.597.1638271839855;
Tue, 30 Nov 2021 03:30:39 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 30 Nov 2021 03:30:39 -0800 (PST)
In-Reply-To: <20211130000937.247@kylheku.com>
Injection-Info: google-groups.googlegroups.com; posting-host=88.224.106.70; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 88.224.106.70
References: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com> <20211130000937.247@kylheku.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a2b20b48-4227-4d36-b0bb-bee3d45a9185n@googlegroups.com>
Subject: Re: Looping through 0..3 randomly
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Tue, 30 Nov 2021 11:30:40 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Oğuz - Tue, 30 Nov 2021 11:30 UTC

On Tuesday, November 30, 2021 at 11:32:54 AM UTC+3, Kaz Kylheku wrote:
> There are 24 permutations; so for instance you could make a program that
> uses a cascade of randomized selection statements to take one of 24
> control flow pathways. That's not going to be compact compared to the
> shuffled array.
>
> The values 0-3 require only two bits, and so a permutation can fit into
> a byte. In 24 bytes of space, you could save all 24 permutations,
> and randomly select one, then extract the values.
>
> What are the values:
>
> 4> (mapcar (op reduce-left (op + @2 (* @1 4))) (perm '(0 1 2 3) 4))
> (27 30 39 45 54 57 75 78 99 108 114 120 135 141 147 156 177 180
> 198 201 210 216 225 228)
>
> So we still have an array, but it's immutable; we aren't having to
> shuffle it. Moreover, a single call to the PRNG gets us the permutation,
> and we are done with the array after making one access to it:
>
> static const unsigned char tab[24] = {
> 27, 30, 39, 45, 54, 57, 75, 78,
> 99, 108, 114, 120, 135, 141, 147, 156,
> 177, 180, 198, 201, 210, 216, 225, 228
> };
>
> int i;
> unsigned char perm = tab[rand() % 24]; /* get a better PRNG */
>
> /* do something with (perm >> 6) */
> /* do something with (perm >> 4) & 3 */
> /* do something with (perm >> 2) & 3 */
> /* do something with perm & 3 */
>
> /* or */
> for (int i = 0; i < 4; i++) {
> /* do something with perm & 3 */
> perm >>= 2;
> }

This is great. I was wasting my time with a LCG algorithm I found online, trying to figure out why it gives me only four permutations... Thank you very much

>
> --
> TXR Programming Language: http://nongnu.org/txr
> Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: Looping through 0..3 randomly

<so54al$di5$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19263&group=comp.lang.c#19263

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: Looping through 0..3 randomly
Date: Tue, 30 Nov 2021 13:09:59 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <so54al$di5$1@dont-email.me>
References: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 30 Nov 2021 12:09:57 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="12e767d8c64a65cb39a16b10eba0337b";
logging-data="13893"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196mIzLFjpPeuuHBlaKIZj+FVUeeCh9wbA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Cancel-Lock: sha1:SL8DzFhr+a1DdJbBwVU48tMtacw=
In-Reply-To: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Tue, 30 Nov 2021 12:09 UTC

Am 30.11.2021 um 08:39 schrieb Oğuz:
> This is how I do it now:
>
> int a[] = { 0, 1, 2, 3 };
> shuffle(a, 4);
> for (int i = 0; i < 4; i++) {
> // do something with `a[i]'
> }
>
> where `shuffle' is a function that I copied from here: https://benpfaff.org/writings/clc/shuffle.html
>
> It works fine, but since I have only four numbers to loop over and they are consequential, putting them into an array and shuffling it feels a bit overkill. Is there an easier way to do this (without using an array, preferably)?
>

Use a proper language:

#include <iostream>
#include <random>

using namespace std;

int main()
{ mt19937_64 mt( (random_device())() );
uniform_int_distribution<unsigned> uid( 0, 3 );
for( unsigned u = 0; i != 4; ++u )
cout << uid( mt ) << endl;
}

Re: Looping through 0..3 randomly

<a4714161-d3e3-4694-acd9-bebe028f9ccen@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19264&group=comp.lang.c#19264

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5ad1:: with SMTP id d17mr50761984qtd.23.1638281357017;
Tue, 30 Nov 2021 06:09:17 -0800 (PST)
X-Received: by 2002:a05:620a:2907:: with SMTP id m7mr9399907qkp.151.1638281356783;
Tue, 30 Nov 2021 06:09:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Tue, 30 Nov 2021 06:09:16 -0800 (PST)
In-Reply-To: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.248.114; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.248.114
References: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a4714161-d3e3-4694-acd9-bebe028f9ccen@googlegroups.com>
Subject: Re: Looping through 0..3 randomly
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Tue, 30 Nov 2021 14:09:17 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Tue, 30 Nov 2021 14:09 UTC

On Tuesday, November 30, 2021 at 4:39:18 AM UTC-3, oguzism...@gmail.com wrote:
> This is how I do it now:
>
> int a[] = { 0, 1, 2, 3 };
> shuffle(a, 4);
> for (int i = 0; i < 4; i++) {
> // do something with `a[i]'
> }
>
> where `shuffle' is a function that I copied from here: https://benpfaff.org/writings/clc/shuffle.html
>
> It works fine, but since I have only four numbers to loop over and they are consequential, putting them into an array and shuffling it feels a bit overkill. Is there an easier way to do this (without using an array, preferably)?

You need to mark in some way the number you already picked. You can use
bit set to have a compact representation. The bit means the position or
index of the number used. You can have 32 or 64 etc.. How many do you need?

Then you generate number from the range you want using rand and
in case rand gives you the number you already picked you can just skip.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main() {
srand((unsigned)time(NULL));

const int range_min = 0;
const int range_max = 32;

unsigned int bits = 0;

for (int i = 0; i < 32;) {
unsigned int u = range_min + rand() % (range_max - range_min);
if ((bits & (1 << u)) != 0) {
//skip
}
else {
bits |= (1 << u);
printf("%u ", u);
i++;
}
}
}

it saves a little memory but it costs some cpu skipping the random numbers
at the end.

Re: Looping through 0..3 randomly

<8735ndfyxo.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19265&group=comp.lang.c#19265

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Looping through 0..3 randomly
Date: Tue, 30 Nov 2021 15:25:23 +0000
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <8735ndfyxo.fsf@bsb.me.uk>
References: <a518c171-93c8-4828-8146-6e3a98d06076n@googlegroups.com>
<20211130000937.247@kylheku.com>
<a2b20b48-4227-4d36-b0bb-bee3d45a9185n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="6899ff236ac028559a992f49b5e20224";
logging-data="4602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191l5KgOayF+tr1WoHNJzGGCfEqA47vvqg="
Cancel-Lock: sha1:Cq6008OskRLEaPaoZ9PRzr8Cddo=
sha1:MBEtkP3PFiHcPgf+B7gBL+2gbXw=
X-BSB-Auth: 1.38d4c07513d88f185f8c.20211130152523GMT.8735ndfyxo.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 30 Nov 2021 15:25 UTC

Oğuz <oguzismailuysal@gmail.com> writes:

> On Tuesday, November 30, 2021 at 11:32:54 AM UTC+3, Kaz Kylheku wrote:
>> There are 24 permutations; so for instance you could make a program that
>> uses a cascade of randomized selection statements to take one of 24
>> control flow pathways. That's not going to be compact compared to the
>> shuffled array.
>>
>> The values 0-3 require only two bits, and so a permutation can fit into
>> a byte. In 24 bytes of space, you could save all 24 permutations,
>> and randomly select one, then extract the values.
>>
>> What are the values:
>>
>> 4> (mapcar (op reduce-left (op + @2 (* @1 4))) (perm '(0 1 2 3) 4))
>> (27 30 39 45 54 57 75 78 99 108 114 120 135 141 147 156 177 180
>> 198 201 210 216 225 228)
>>
>> So we still have an array, but it's immutable; we aren't having to
>> shuffle it. Moreover, a single call to the PRNG gets us the permutation,
>> and we are done with the array after making one access to it:
>>
>> static const unsigned char tab[24] = {
>> 27, 30, 39, 45, 54, 57, 75, 78,
>> 99, 108, 114, 120, 135, 141, 147, 156,
>> 177, 180, 198, 201, 210, 216, 225, 228
>> };
>>
>> int i;
>> unsigned char perm = tab[rand() % 24]; /* get a better PRNG */
>>
>> /* do something with (perm >> 6) */
>> /* do something with (perm >> 4) & 3 */
>> /* do something with (perm >> 2) & 3 */
>> /* do something with perm & 3 */
>>
>> /* or */
>> for (int i = 0; i < 4; i++) {
>> /* do something with perm & 3 */
>> perm >>= 2;
>> }
>
> This is great. I was wasting my time with a LCG algorithm I found
> online, trying to figure out why it gives me only four
> permutations... Thank you very much

You can also permute an array based on the "index" of the permutation:

void permute_from_index(long long unsigned idx, int n, int *a)
{
long long unsigned fact = factorial(n);
for (int i = 0; i < n; i++) {
fact /= n - i;
swap(i, i + idx / fact, a);
idx = idx % fact;
}
}

No extra storage needed. Just pick a number between 0 and n!-1 and call
the function. Of course, if you've already calculated n!, you don't
need to calculate it again, but it fast for the range of n that will be
practical (n <= 20).

--
Ben.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor