Generate a random number in the range 1 - 10

前端 未结 7 1974
-上瘾入骨i
-上瘾入骨i 2020-12-04 11:32

Since my approach for a test query which I worked on in this question did not work out, I\'m trying something else now. Is there a way to tell pg\'s random() fu

相关标签:
7条回答
  • 2020-12-04 11:50

    To summarize and a bit simplify, you can use:

    -- 0 - 9
    select floor(random() * 10);
    -- 0 - 10
    SELECT floor(random() * (10 + 1));
    -- 1 - 10
    SELECT ceil(random() * 10);
    

    And you can test this like mentioned by @user80168

    -- 0 - 9
    SELECT min(i), max(i) FROM (SELECT floor(random() * 10) AS i FROM generate_series(0, 100000)) q;
    -- 0 - 10
    SELECT min(i), max(i) FROM (SELECT floor(random() * (10 + 1)) AS i FROM generate_series(0, 100000)) q;
    -- 1 - 10
    SELECT min(i), max(i) FROM (SELECT ceil(random() * 10) AS i FROM generate_series(0, 100000)) q;
    
    0 讨论(0)
  • 2020-12-04 11:50

    (trunc(random() * 10) % 10) + 1

    0 讨论(0)
  • 2020-12-04 11:53

    If by numbers between 1 and 10 you mean any float that is >= 1 and < 10, then it's easy:

    select random() * 9 + 1
    

    This can be easily tested with:

    # select min(i), max(i) from (
        select random() * 9 + 1 as i from generate_series(1,1000000)
    ) q;
           min       |       max
    -----------------+------------------
     1.0000083274208 | 9.99999571684748
    (1 row)
    

    If you want integers, that are >= 1 and < 10, then it's simple:

    select trunc(random() * 9 + 1)
    

    And again, simple test:

    # select min(i), max(i) from (
        select trunc(random() * 9 + 1) as i from generate_series(1,1000000)
    ) q;
     min | max
    -----+-----
       1 |   9
    (1 row)
    
    0 讨论(0)
  • 2020-12-04 11:56

    The correct version of hythlodayr's answer.

    -- ERROR:  operator does not exist: double precision % integer
    -- LINE 1: select (trunc(random() * 10) % 10) + 1
    

    The output from trunc has to be converted to INTEGER. But it can be done without trunc. So it turns out to be simple.

    select (random() * 9)::INTEGER + 1
    

    Generates an INTEGER output in range [1, 10] i.e. both 1 & 10 inclusive.

    For any number (floats), see user80168's answer. i.e just don't convert it to INTEGER.

    0 讨论(0)
  • 2020-12-04 12:00

    If you are using SQL Server then correct way to get integer is

    SELECT Cast(RAND()*(b-a)+a as int);
    

    Where

    • 'b' is the upper limit
    • 'a' is lower limit
    0 讨论(0)
  • 2020-12-04 12:05

    This stored procedure inserts a rand number into a table. Look out, it inserts an endless numbers. Stop executing it when u get enough numbers.

    create a table for the cursor:

    CREATE TABLE [dbo].[SearchIndex](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Cursor] [nvarchar](255) NULL) 
    

    GO

    Create a table to contain your numbers:

    CREATE TABLE [dbo].[ID](
    [IDN] [int] IDENTITY(1,1) NOT NULL,
    [ID] [int] NULL)
    

    INSERTING THE SCRIPT :

    INSERT INTO [SearchIndex]([Cursor])  SELECT N'INSERT INTO ID  SELECT   FLOOR(rand() * 9 + 1)  SELECT COUNT (ID) FROM ID
    

    CREATING AND EXECUTING THE PROCEDURE:

    CREATE PROCEDURE [dbo].[RandNumbers] AS
    BEGIN
    Declare  CURSE  CURSOR  FOR (SELECT  [Cursor] FROM [dbo].[SearchIndex]  WHERE [Cursor] IS NOT NULL)
    DECLARE @RandNoSscript NVARCHAR (250)
    OPEN CURSE
    FETCH NEXT FROM CURSE
    INTO @RandNoSscript 
    WHILE @@FETCH_STATUS IS NOT NULL 
    BEGIN
    Print @RandNoSscript
    EXEC SP_EXECUTESQL @RandNoSscript;  
     END
     END
    GO
    

    Fill your table:

    EXEC RandNumbers
    
    0 讨论(0)
提交回复
热议问题