如何连续生成随机负整数和正整数的文件?
我想要一个随机生成的正或负序列整数的文件。 目前,我要求文件包含大致(不需要保证)相同数量的负数和正数,但以后可以轻松更改比例。 我所说的“串行”是指第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果我理解正确的话,你想随机交错正整数和负整数。 例如:
1 2 -1 3 -2 4 5- 3
。更新:
要更改比例,我会这样做:
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
.Update:
To change proportions I'd do this:
The
get_list_generator()
function returns a closure. This way you can even have multiple list generators going at once.我们开始高尔夫比赛吧? (44)
编辑:daotoad的 40个字符版本
Let's start golf contest? (44)
Edit: daotoad's 40 chars version
其中
15
是生成的数字总数,tp
是您想要的正数数量(有效表示正数/负数的比率):Where
15
is the total amount of numbers generated andtp
is the amount of positive numbers you want (effectively indicating the ratio of pos/neg):我无法将其完全融入到一句台词中。 还有谁?
编辑:啊,单行,65 个字符(如果您在同一个 shell 中重复调用它,则需要设置 a 和 b):
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):
这是受 lhunath 和 Brian 的答案启发的 Bash 俏皮话(2?)。
这是参加高尔夫比赛 (44) 的 Awk 脚本。
这是更清晰的惯用方式:
Here's a Bash one-liner (2?) inspired by lhunath and Brian's answers.
Here's an Awk script that competes in the golf contest (44).
This is the clearer idiomatic way to write it:
没有一组数字可以满足您的所有标准。 你不能说你想要随机,但同时又说第 k 个负值 == -k 和第 k 个正值 == k。 你可以选择随机,也可以不选择。
至于您想要做什么,为什么不将这两个问题分开并在诸如长度为 n 的整数对数组之类的东西上测试排序。 其中第一个可以是 0 或 1,第二个将是您的稳定性跟踪器(只是从 0 到 n 的计数)。
生成所需的 0 和 1 列表,对它们进行混洗,然后添加到跟踪器整数上。 现在按第一个元素对这些对进行排序。
您的排序的输入将如下所示。
稳定排序会产生这种情况,
不稳定排序会产生 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.
Stable sorts will produce this
Unstable ones will produce the 0's and 1's with the tracker integers out of order.