Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1076879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:29:40+00:00 2026-05-16T21:29:40+00:00

I want to randomly choose a value from a small range of integers (less

  • 0

I want to randomly choose a value from a small range of integers (less than 200). As an alternative to

SELECT RAND()

I’m trying to use

CAST(CAST(CRYPT_GEN_RANDOM(2) AS INTEGER) AS FLOAT) / 65535

but I’m getting some strange effects.

For example:

WITH Numbers (num)
     AS
     (
      SELECT num
        FROM (
              VALUES (1), (2), (3), (4), 
                     (5), (6), (7), (8), 
                     (9), (10)
             ) AS Numbers (num)
     ), 
     RandomNumber (num)
     AS 
     (
      SELECT CAST(
                  (CAST(CAST(CRYPT_GEN_RANDOM(2) AS INTEGER) AS FLOAT) / 65535) 
                     * (SELECT COUNT(*) FROM Numbers) + 1 
                  AS INTEGER
                 )
     )
SELECT T1.num, R1.num
  FROM Numbers AS T1 
       INNER JOIN RandomNumber AS R1
          ON T1.num = R1.num;

I’d expect this to return exactly one row with both column values equal.

However, it returns zero, one or more rows, with the column values being only occasionally equal.

Any idea what’s going on here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:29:41+00:00Added an answer on May 16, 2026 at 9:29 pm

    Try this:

    select abs(checksum(newid()))%200
    

    Update

    Something fishy is going on here:

    select 'Not in the range! This is impossible!' [Message]
    where cast(CAST(CAST(CRYPT_GEN_RANDOM(2) AS int) AS float) / 65535 * 10  as int) 
    not in (0,1,2,3,4,5,6,7,8,9)
    

    Hit F5 until you get the message.

    Update 2

    This query has the same effect:

    select 'Not in the range! This is impossible!' [Message]
    where abs(checksum(newid()))%10
    not in (0,1,2,3,4,5,6,7,8,9)
    

    Update 3

    select N'WTF? Schrödinger''s cat!' [Message], *
    from (select 0 union select 1) t(n)
    join (select abs(checksum(newid()))%2 rnd) r(n) on t.n = r.n
    

    And this last query may sometimes return 2 rows which essentially means that abs(checksum(newid()))%2 returns 0 and 1 at the same time which is impossible as it is included in “select one row only” statement hence the value gets updated later after the join has been made. 😐 And then the join occurs again which is flabbergasting.

    Update 4

    It is a known bug at Microsoft Connect. And here’s an insightful article on what is happening.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to choose an entry from my database randomly, but not on every
I want to choose randomly from among 4 directions: movePlayer(map, &positionPlayer, direction); where direction
i want to create a quiz application. the questions should be randomly selected from
I want to post all text box responses to results.php and then randomly choose
In views.py I want to randomly choose one record with my filter: a=Entry.objects.filter(first_name__contains='Br')).order_by('?')[0] b=a.id
Suppose I want to randomly select a number n between 0 and 30, where
Suppose I have a list of elements and I want to randomly select an
I want a javascript script that choose either value1 or value2 randomly, not between
I want to select a number of random words from an array to make
I want to choose randomly* between two alternatives with unequal probability. For instance, when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.