在sqlite中生成多个随机数

发布于 2024-11-06 14:32:43 字数 266 浏览 1 评论 0原文

我想在 Sqlite 中生成 N 组随机数。

现在我得到的最好的办法就是像这样联合一组对 random() 的调用

create view random_view as
select random()
union select random()
union select random()
union select random()
union select random()
;

也许有人有一个更聪明的解决方案可以生成 N 个数字?

I'd like to generate size N set of random numbers in Sqlite.

Right now the best I've got is to union a set of calls to random() like this

create view random_view as
select random()
union select random()
union select random()
union select random()
union select random()
;

Maybe someone has a more clever solution that could generate N numbers?

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

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

发布评论

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

评论(5

无人接听 2024-11-13 14:32:43
create temp table rtab (x integer primary key, v default (random()));

create temp trigger rtrig 
before insert on rtab
when new.x > 0 begin
   insert or replace into rtab (x) values (new.x - 1); 
end;

PRAGMA recursive_triggers = on;

insert into rtab (x) values (9);

sqlite> select * from rtab;
0|-6742468521271879323
1|-8020364160821973904
2|4567559116463208288
3|5330277995838989553
4|-9000358059551141276
5|-7148307065140334921
6|8156512560793181351
7|-10751076681323044
8|-7335834651732027766
9|6837665741304560539
sqlite> 
create temp table rtab (x integer primary key, v default (random()));

create temp trigger rtrig 
before insert on rtab
when new.x > 0 begin
   insert or replace into rtab (x) values (new.x - 1); 
end;

PRAGMA recursive_triggers = on;

insert into rtab (x) values (9);

sqlite> select * from rtab;
0|-6742468521271879323
1|-8020364160821973904
2|4567559116463208288
3|5330277995838989553
4|-9000358059551141276
5|-7148307065140334921
6|8156512560793181351
7|-10751076681323044
8|-7335834651732027766
9|6837665741304560539
sqlite> 
ˉ厌 2024-11-13 14:32:43

没试过,但也许这个?给定至少包含 N 行的任何表,

SELECT RANDOM() FROM anytable LIMIT ?

? 替换为 N。

Didn't try, but maybe this? Given any table of at least N rows,

SELECT RANDOM() FROM anytable LIMIT ?

replacing ? with N.

千紇 2024-11-13 14:32:43

如果您需要数百万行并且有 shell/bash,这样可能就足够了:

echo -e '.import "/dev/stdin" mytable\n';睡眠 0.5 ;对于 {1..10000000} 中的 i;执行 echo $RANDOM;完成)| sqlite3 my.db

睡眠步骤对于防止 sqlite 由于缓冲而将第一个数字读取为 SQL 命令至关重要。青年MMV

If you need millions of rows and have shell/bash, it might be good enough this way:

echo -e '.import "/dev/stdin" mytable\n'; sleep 0.5 ; for i in {1..10000000}; do echo $RANDOM; done) | sqlite3 my.db

The sleep step is critical to prevent sqlite from reading the first numbers as SQL commands due to buffering. YMMV

野生奥特曼 2024-11-13 14:32:43

您可以使用递归查询

此查询生成一个包含 1000 个随机数的表:

CREATE TABLE test(field1);

INSERT INTO test
  WITH RECURSIVE
    cte(x) AS (
       SELECT random()
       UNION ALL
       SELECT random()
         FROM cte
        LIMIT 1000
  )
SELECT x FROM cte;

You can use a recursive query.

This query generates a table with 1000 random numbers:

CREATE TABLE test(field1);

INSERT INTO test
  WITH RECURSIVE
    cte(x) AS (
       SELECT random()
       UNION ALL
       SELECT random()
         FROM cte
        LIMIT 1000
  )
SELECT x FROM cte;
高冷爸爸 2024-11-13 14:32:43

使用 generate_series() 表函数。文档显示了这个生成 20 个随机整数值的示例:

SELECT random() FROM generate_series(1,20);

Use the generate_series() table function. The docs show this example which generates 20 random integer values:

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