【问题标题】:BigQuery RAND() not accepting seed valueBigQuery RAND() 不接受种子值
【发布时间】:2020-11-20 16:08:53
【问题描述】:

我正在尝试使用 RAND() 函数并设置一个种子,以在https://cloud.google.com/dataprep/docs/html/RAND-Function_57344757#int_value 之后的输出中生成一组一致的 10 个随机值

通过这个查询我得到了错误:No matching signature for function RAND for argument types: INT64.

rand_val as (
  select value
  from test
  where RAND(3) < 10/ (select count(value) from test))

select * from rand_val

我在这里做错了什么?

【问题讨论】:

    标签: sql random google-bigquery random-seed


    【解决方案1】:

    在 BigQuery 中,rand() 不采用种子参数。检查documentation

    您可以使用伪随机数生成器。例如,如果您的表有一个主键,您可以使用 farm_fingerprint() 获得 10 个带有键的“随机”样本:

    with t as (
          select t.*,
                 row_number() over (order by farm_fingerprint(concat(pk, '3')) ) as seqnum
          from t
         )
    select t.*
    from t
    where seqnum <= 10;
    

    concat() 的第二个参数是种子值。

    【讨论】:

    • 有人知道这些“随机”选择的质量吗?为模拟创建数据或基于样本估计总体参数是否好?它是否通过了 Marsaglia 的测试?
    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多