Sql Server Generate 6 Digit Random Number

3 views
Skip to first unread message

Theodor Urena

unread,
Aug 5, 2024, 12:56:45 AM8/5/24
to walkjonyre
RANDreturns a number between 0 and 1. So you don't require a call to ABS(). You can get a 10 digit random integer by multiplying the result of RAND by 10 to the power of 10 (10000000000) and then rounding the result (I have choosen floor in the example below but you could use CEILING() or ROUND()). Since 10 digits is at the limit of the int data type I'm casting POWER()'s as bigint.

To satisfy security requirements, I need to find a way to replace SSN's with unique, random 9 digit numbers, before providing said database to a developer. The SSN is in a column in a table of a database. There may be 10's of thousands of rows in said table. The number does not need hyphens. I am a beginner with SQL and programming in general.


If the requirement is to obfuscate a database then this will return the same unique value for each distinct SSN in any table preserving referential integrity in the output without having to do a lookup and translate.


Generate a list of all the numbers you haven't assigned yet. Shuffle the list and create a new table containing the random ID and an incrementing index. When you need a new ID, select the one with the minimum index; remove it from the unused ID table and return it as your new ID.


Third option is to use GUIDs as unique identifiers. They are quite computationally efficient and guaranteed to be unique even crossing the domain boundaries. However, they are something about 80 symbols, not just 6 :)


In my experience it's usually because managers don't want scenarios like "Bob was hired just after Alice, Alice's employee id is 1987 so Bob's must be 1988". My answer to this is "so what?". Knowing Bob's employee ID shouldn't help an attacker - if it does then there are much bigger problems in the organisation's security.


You should also create either a primary key constraint or a unique constraint to enforce the uniqueness as well as possibly a check constraint to enforce the lower and upper bounds (100000 and 999999, respecitively).


Does this absolutely have to be a 6 digit number? Can you use a uniqueidentifier, as suggested elsewhere? The NEWID() function means you don't get serialisation issues of trying to generate a unique number for multiple sessions at the same time.


If you can tell us the entire story of what you want we don't have to keep coming back with more questions. So far we know you want a 7 digit random that must be unique. Do you have a table this belong in or this a separate table of these values? Do you need to generate these one at a time (like for an insert) or do you need a big list of them? The reason is because depending on the needs it will greatly affect how to go about this.


There are exactly 8,999,999 distinct integers between 1,000,000 and 9,999,999. Depending on what these numbers are being generated for, you could start having collisions reasonably soon (thanks to our good friend the birthday paradox). If the only criteria is length and uniqueness, and identity column starting at 1000000 with a check constraint limiting it to less than 10,000,000 would constraint it such.


Be it playing a game or in development ,most of us will come across a situation to generate random numbers . So it is good to know some functions in advance to handle random number situations .The best example of random number generation is the OTP or One Time Password that we receive during any application signup or while making a payment .In this post let us find some interesting ways to generate random numbers and also few points about Rand() Function.


The NewID() function is generally used to create a GUID in Sql server .As the definition says the term GUID stands for Global Unique Identifier and it is also known as UNIQUEIDENTIFIER. GUID is a 16 byte binary SQL Server data type that is globally unique across tables, databases, and servers. The data type of GUID is UNIQUEIDENTIFIER in SQL Server.


Now this GUID can be converted to an integer value using CHECKSUM() function. The CHECKSUM() function usually computes a hash value over its arguments and also it returns integer value . Hence we use this here .


My major gripe with RAND() is what happens if we want to generate a random number for every row of a query, lets see what happens if we run the following against our trusty old workhorse, AdventureWorks and try to assign a random number to everyone in the person table.


For a few rows it is ok, but when i try to generate a huge list then it gives some duplicates, because some negative number might become positive resulting in a duplicate key . Other than this , nice way of generating random numbers


I am looking to create a 4 digit random number generator for a variable. Is there a specific variable type I should use? Right now I have single line variable and when I put the below script in the default value, it generates the number but there is a decimal behind it.

javascript: Math.round(Math.random()*9000) + 1000;



Could anyone answer how to truncate the decimal and what is behind that? Or if there is an easier way to make a random integer pop up in a variable?



Thanks in advance.


I originally said use Math.floor(), however Math.round() should have the same truncating qualities. I tested your code and didnt have anything appearing to the right of the decimal point. If your code still doesn't work, you can always use parseInt on the result.


window.crypto.getRandomValues() is used in browsers but doesn't appear to be available server-side in servicenow. Is there some other built-in source for strong random numbers for servicenow server-side scripts? What about other crypto services like hashing?


This article explains how to generate random numbers or select random numbers within different ranges in SQL Server databases with syntax and examples. The detailed description of steps and explanatory screenshots will give you an idea of how you can perform tasks using dbForge Data Generator for SQL Server.


dbForge Data Generator for SQL Server is a visual data generation tool that enables you to populate SQL databases with random test data quickly and easily. First, you select the database you want to populate with data and set up data generation options. Next, you select tables and columns to be populated. The tool analyzes the column name, its data type, and properties, and automatically assigns the appropriate data generator. In the case of relationships between tables, dbForge Data Generator can preserve data integrity and consistency of your databases. Once done, you can start working with test data, for example, use it for load testing or export a sql file to a csv file.


The RANDOM function generates a random decimal number from 0 (inclusive) through 1 (exclusive) or within the specified range. The syntax of the SQL random function is as follows: RAND([seed]) where seed is an optional parameter that refers to the tinyint, smallint, or int data type. If you do not specify a seed value, SQL Server generates a random number. When specified, the function returns the same sequence of random numbers within the session. If you want to receive a different value, you should use either different sessions or different seed values.


Let's compare two examples and see how the RAND() function generates a random number in decimals between 0 to 1. In the first example, we omit the seed, while in the second one, we specify the value (5) for the seed.


The ROUND function in SQL returns a number rounded to the specified number of decimal. The ROUND function can be used along with the RAND function when you need to generate a random value within the specified range (m, n). The syntax is as follows:


Next, let's demonstrate how you can use the ROUND function along with the RAND function to generate a random decimal number within the specified range. In the example, we select the range of numbers between 1 to 10. The decimal value will be round to one decimal place.


In the example, we are going to generate a random number between 1 and 10 in SQL Server. It should be noted that the random decimals to be returned will be greater than 1 and less than 10 but will not be equal to 1 or 10.


Let's now generate a random integer number within the range of 1 and 1000 where 1 is the minimum value and 1000 is the maximum value. As mentioned, whenever you execute the query, SQL Server will return different random integer numbers.


Except for calculating random numbers, we can use the RANDOM function for the operations with database objects, for example, to sort a SQL query list in random order. To do that, in SQL Server, we need to use the NEWID function in the ORDER BY clause. However, keep in mind that it would be better to use it for short lists. Otherwise, SQL performance may get worse.


In practice, it is much easier to use a SQL data generator tool to generate random numbers, especially when you work with a huge volume of data. The Devart team developed the top data generator tool as part of the dbForge product line for SQL Server - dbForge Data Generator for SQL Server. It is a powerful GUI tool that enables you to generate test data of any complexity and volume within a few clicks. Moreover, the tool contains all the required features and generators to cut the time needed for data generation and turn it into a real pleasure. Among its main features, you can also evaluate the following:


We use cookies to provide you with a better experience on the Devart website. You can read more about our use of cookies in our Cookies Policy.

Click OK to continue browsing the Devart site. Be aware you can disable cookies at any time.


Does anyone have code; looking for a random number function generator.I would just supply with a variable length @NumberLength = 50, etc.It can create numbers up to 50 digits, and store in varchar. (Bigint does not store this high)I am using Floor Rand, Floor, NewId(), still not receiving answer, that exceeds 12 digits.


The maximum bigint is 9223372036854775807 so clearly there is a lower probability that the leftmost digit will be 9. Similarly there is a greater probability that the second character will be 1 or 0 than any other digit. As it is possible for the second character to be 3 only if the leading character is 0-8. Similar issues exist for the other positions but decline in importance as you move rightwards.

3a8082e126
Reply all
Reply to author
Forward
0 new messages