如何连续生成随机负整数和正整数的文件?

发布于 2024-07-16 02:44:35 字数 710 浏览 4 评论 0原文

我想要一个随机生成的正或负序列整数的文件。 目前,我要求文件包含大致(不需要保证)相同数量的负数和正数,但以后可以轻松更改比例。 我所说的“串行”是指第 k 个随机负数等于 -k,第 k 个随机正数等于 +k。

这个 GNU Bash 脚本单行脚本将满足文件格式,但不会是随机

$ seq -1 -1 -5 && seq 1 5
-1
-2
-3
-4
-5
1
2
3
4
5

这个例子显示了我正在寻找的更好的东西,但仍然不是随机的,因为整数可预测地在负数和正数之间交替。

$ paste <(seq -1 -1 -5) <(seq 1 5) | tr '\t' '\n'
-1
1
-2
2
-3
3
-4
4
-5
5

通过 shuf 命令发送其中之一会使它们随机为负或正,但它们会失去串行性。

$ paste <(seq -1 -1 -5) <(seq 1 5) | tr '\t' '\n' | shuf
-5
4
3
2
-2
1
-1
-4
5
-3

注意:我正在尝试测试对位列表/数组(零和一)进行排序的算法,但如果我使用 0 和 1,我将无法分析排序的行为或判断是否保留了稳定性。

I want a file of randomly generated positive or negative serial integers. For now, I ask the file contain roughly (no guarantee required) equal numbers of negative and positive, but make it easy to change the proportions later. By "serial", I mean the kth random negative is equal to -k, and the kth random positive is equal to +k.

This GNU Bash script one-liner would satisfy the file format, but just wouldn't be random.

$ seq -1 -1 -5 && seq 1 5
-1
-2
-3
-4
-5
1
2
3
4
5

This example shows what I'm looking for even better, but is still not random since the integers alternate predictably between negative and positive.

$ paste <(seq -1 -1 -5) <(seq 1 5) | tr '\t' '\n'
-1
1
-2
2
-3
3
-4
4
-5
5

Sending one of these through the shuf command makes them randomly negative or positive, but they lose their serial-ness.

$ paste <(seq -1 -1 -5) <(seq 1 5) | tr '\t' '\n' | shuf
-5
4
3
2
-2
1
-1
-4
5
-3

Note: I'm trying to test algorithms that sort lists/arrays of bits (zeros and ones), but if I use 0s and 1s I won't be able to analyse the sort's behaviour or tell if stability was preserved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凤舞天涯 2024-07-23 02:44:35

如果我理解正确的话,你想随机交错正整数和负整数。 例如:1 2 -1 3 -2 4 5- 3

my $count = 10;
my $pos   =  1;
my $neg   = -1;

my @random = map { 
    int(rand 2) 
    ? $pos++ 
    : $neg--
} 1..$count; 

print "@random\n";

更新:

要更改比例,我会这样做:

use strict;
use warnings;

my $next = get_list_generator(.5);

my @random = map $next->(), 1..10; 
print "@random\n";

my $again = get_list_generator(.25);

my @another = map $again->(), 1..10; 
print "@another\n";

sub get_list_generator {
    my $prob_positive = shift;

    my $pos = 1;
    my $neg = -1;

    return sub {
        return rand() <= $prob_positive ? scalar $pos++ : scalar $neg--;
    }

}

get_list_generator()函数返回一个闭包。 这样你甚至可以同时运行多个列表生成器。

If I understand correctly, you want to interleave the positive integers and the negative integers randomly. For example: 1 2 -1 3 -2 4 5- 3.

my $count = 10;
my $pos   =  1;
my $neg   = -1;

my @random = map { 
    int(rand 2) 
    ? $pos++ 
    : $neg--
} 1..$count; 

print "@random\n";

Update:

To change proportions I'd do this:

use strict;
use warnings;

my $next = get_list_generator(.5);

my @random = map $next->(), 1..10; 
print "@random\n";

my $again = get_list_generator(.25);

my @another = map $again->(), 1..10; 
print "@another\n";

sub get_list_generator {
    my $prob_positive = shift;

    my $pos = 1;
    my $neg = -1;

    return sub {
        return rand() <= $prob_positive ? scalar $pos++ : scalar $neg--;
    }

}

The get_list_generator() function returns a closure. This way you can even have multiple list generators going at once.

﹎☆浅夏丿初晴 2024-07-23 02:44:35

我们开始高尔夫比赛吧? (44)

perl -le'print rand>.5?++$a:--$b for 1..10'

编辑daotoad的 40个字符版本

seq 1 10|perl -ple'$_=rand>.5?++$a:--$b'

Let's start golf contest? (44)

perl -le'print rand>.5?++$a:--$b for 1..10'

Edit: daotoad's 40 chars version

seq 1 10|perl -ple'$_=rand>.5?++$a:--$b'
泪之魂 2024-07-23 02:44:35

其中 15 是生成的数字总数,tp 是您想要的正数数量(有效表示正数/负数的比率):

tp=8
unset p n
for i in $(printf '%s\n' {1..15} | gsort -R); do
    (( i <= tp )) && \
        echo $((++p)) || \
        echo $((--n))
done

Where 15 is the total amount of numbers generated and tp is the amount of positive numbers you want (effectively indicating the ratio of pos/neg):

tp=8
unset p n
for i in $(printf '%s\n' {1..15} | gsort -R); do
    (( i <= tp )) && \
        echo $((++p)) || \
        echo $((--n))
done
〃安静 2024-07-23 02:44:35
#!/bin/bash

pos=0 neg=0
for i in {1..10}
do 
    if (( ($RANDOM > 16384 ? ++pos : --neg) > 0 ))
    then echo $pos
    else echo $neg
    fi
done

我无法将其完全融入到一句台词中。 还有谁?

编辑:啊,单行,65 个字符(如果您在同一个 shell 中重复调用它,则需要设置 a 和 b):

a=0 b=0;for i in {1..10}; do echo $(($RANDOM>16384?++a:--b));done
#!/bin/bash

pos=0 neg=0
for i in {1..10}
do 
    if (( ($RANDOM > 16384 ? ++pos : --neg) > 0 ))
    then echo $pos
    else echo $neg
    fi
done

I could not quite fit this into a one-liner. Anyone else?

edit: Ah, a one liner, 65 characters (need to set a and b if you're repeatedly invoking this in the same shell):

a=0 b=0;for i in {1..10}; do echo $(($RANDOM>16384?++a:--b));done
不忘初心 2024-07-23 02:44:35

这是受 lhunath 和 Brian 的答案启发的 Bash 俏皮话(2?)。

RANDOM=$; pos=1; neg=-1; for i in {1..10}; do \
echo $(( $(echo $RANDOM / 32767 \> 0.5 | bc -l) ? pos++ : neg-- )); done

这是参加高尔夫比赛 (44) 的 Awk 脚本。

seq 1 10|awk '{print(rand()>0.5?++p:--n);}'

这是更清晰的惯用方式:

seq 1 10 | awk 'BEGIN{srand(); pos=1; neg=-1;}
                {print (rand() > 0.5 ? pos++ : neg--);}'

Here's a Bash one-liner (2?) inspired by lhunath and Brian's answers.

RANDOM=$; pos=1; neg=-1; for i in {1..10}; do \
echo $(( $(echo $RANDOM / 32767 \> 0.5 | bc -l) ? pos++ : neg-- )); done

Here's an Awk script that competes in the golf contest (44).

seq 1 10|awk '{print(rand()>0.5?++p:--n);}'

This is the clearer idiomatic way to write it:

seq 1 10 | awk 'BEGIN{srand(); pos=1; neg=-1;}
                {print (rand() > 0.5 ? pos++ : neg--);}'
盗梦空间 2024-07-23 02:44:35

没有一组数字可以满足您的所有标准。 你不能说你想要随机,但同时又说第 k 个负值 == -k 和第 k 个正值 == k。 你可以选择随机,也可以不选择。

至于您想要做什么,为什么不将这两个问题分开并在诸如长度为 n 的整数对数组之类的东西上测试排序。 其中第一个可以是 0 或 1,第二个将是您的稳定性跟踪器(只是从 0 到 n 的计数)。

生成所需的 0 和 1 列表,对它们进行混洗,然后添加到跟踪器整数上。 现在按第一个元素对这些对进行排序。

您的排序的输入将如下所示。

0, 1
1, 2
0, 3
1, 4
1, 5
0, 6
0, 7
1, 8
1, 9
1, 10
0, 11
0, 12
0, 13

稳定排序会产生这种情况,

0, 1
0, 3
0, 6
0, 7
0, 11
0, 12
0, 13
1, 2
1, 4
1, 5
1, 8
1, 9
1, 10

不稳定排序会产生 0 和 1,且跟踪器整数无序。

There is no set of numbers that will fit all your criterion. You can't say you want random but at the same time say that the kth negative value == -k and the kth positive value == k. You can either have it random, or not.

As to what you're trying to do, why not separate the two concerns and test the sort on something like an array of pairs of integers length n. The first of the pair can be zero or 1 and the second of the pair will be your stability tracker (just a count from 0 to n).

Generate the list of 0's and 1's that you want and shuffle them then add on the tracker integer. Now sort the pairs by their first element.

The input to your sort will look something like this.

0, 1
1, 2
0, 3
1, 4
1, 5
0, 6
0, 7
1, 8
1, 9
1, 10
0, 11
0, 12
0, 13

Stable sorts will produce this

0, 1
0, 3
0, 6
0, 7
0, 11
0, 12
0, 13
1, 2
1, 4
1, 5
1, 8
1, 9
1, 10

Unstable ones will produce the 0's and 1's with the tracker integers out of order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文