Inin the SQL language, the random function newid and rand () are not used in the same way, the following will give you a sample analysis of the differences between these two random functions, for your reference, you want to understand the SQL function can play a role. Example: SELECT * from TABLENAMEORDER by NEWID () --Random sort select TOP * from TABLENAME ORDER by NEWID ()--take 10 records randomly from the TABLENAME table example: use NEWID functions for variables the following Example uses NEWID () to assign a value to a variable declared as a uniqueidentifier data type. The value is output before testing the value of the uniqueidentifier data type variable. --Creating a local variable with declareset syntax. declare @myid uniqueidentifier set @myid = NEWID () &nb Sp PRINT ' Value of @myid is ' + CONVERT (varchar (255), @myid) Here is the result set: value of @myid is 6f9619ff-8b86-d011-b42d-00c0 4fc964ff Note: newid returns different values for each computer. The figures shown are only useful for interpreting. Random functions: Rand () Execute in Query Analyzer: Select rand (), You can see that the result would be a random decimal number like this: 0.776282033621286, decimals like this are not used much in real-world applications, and random integers are usually taken to take arbitrary numbers. Look at the following two methods of randomly taking integers: 1, method one a:select floor (rand () *n)---The number generated is this: 12.0 b:select cast (Floor (rand () *n) as int)---The number generated is this: 12 2, method two a:select ceiling (rand () * N)---Generated number is this: 12.0 b:select cast (Ceiling (rand () * n) as int) The number that is generated by the---is this:12 where n is an integer that you specify, such as 100, you can see that the a method of the two methods is a decimal with the. 0, and the B method is the real integer. Roughly, these two methods are no different, really no difference? In fact, there is a point, that is, their generation of random numbers of the fanThe number range of the: Method 1:0 to N-1, such as CAST (Floor (rand () *100) as int) will generate a range of numbers from 0 to 99 for any integer Method 2: Between 1 and N, as cast (ceiling (rand () * as int) will generate any integer between 1 and 100 for this difference, seethe online Help for SQL is a cicada:Compare the CEILING and floor CEILING functions to return the smallest integer greater than or equal to the given number expression. The floor function returns the largest integer less than or equal to the given number expression. For example, for a numeric expression 12.9273,ceiling will return 13,floor will return 12. Floor and CEILING The data type of the return value is the same as the data type of the input numeric expression. Now, you can use these two methods to get the random number according to your own needs. ^_^ Finally, let's use a combination of NEWID () and Rand (): Example: Fromread N (1~100) data in data table [TABLENAME], and randomly sortSelect TOP (select cast (Ceiling () (rand ()) as int) * FROM TABLENAME ORDER by NEWID ()
NewID () and Rand ()