{"id":6110,"date":"2018-07-11T09:50:54","date_gmt":"2018-07-11T14:50:54","guid":{"rendered":"https:\/\/www.bhinfo.com.mx\/cursos\/?p=6110"},"modified":"2018-07-11T10:02:01","modified_gmt":"2018-07-11T15:02:01","slug":"sql-numeros-aleatorios","status":"publish","type":"post","link":"https:\/\/www.bhinfo.com.mx\/cursos\/2018\/07\/11\/sql-numeros-aleatorios\/","title":{"rendered":"SQL, N\u00fameros aleatorios"},"content":{"rendered":"<p>M\u00e9todo 1 :&nbsp; Generar un n\u00famero aleatorio entre un rango<\/p>\n<div>\n<pre class=\"lang:tsql decode:true \">---- Create the variables for the random number generation\r\nDECLARE @Random INT;\r\nDECLARE @Upper INT;\r\nDECLARE @Lower INT\r\n&nbsp;\r\n---- This will create a random number between 1 and 999\r\nSET @Lower = 1 ---- The lowest random number\r\nSET @Upper = 999 ---- The highest random number\r\nSELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)\r\nSELECT @Random<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>M\u00e9todo 2 : Generar un n\u00famero aleatorio flotante<\/p>\n<div>\n<pre class=\"lang:tsql decode:true\">SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )\r\n+ (DATEPART(ss, GETDATE()) * 1000 )\r\n+ DATEPART(ms, GETDATE()) )<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>M\u00e9todo 3 : Generar un n\u00famero aleatorio r\u00e1pido<\/p>\n<div>\n<pre class=\"lang:tsql decode:true \">---- random float from 0 up to 20 - [0, 20)\r\nSELECT 20*RAND()\r\n-- random float from 10 up to 30 - [10, 30)\r\nSELECT 10 + (30-10)*RAND()\r\n--random integer BETWEEN 0\r\nAND 20 - [0, 20]\r\nSELECT CONVERT(INT, (20+1)*RAND())\r\n----random integer BETWEEN 10\r\nAND 30 - [10, 30]\r\nSELECT 10 + CONVERT(INT, (30-10+1)*RAND())\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>M\u00e9todo 4 :&nbsp;<span id=\"result_box\" class=\"short_text\" lang=\"es\"><span class=\"\">N\u00fameros aleatorios (Float, Int) Tablas basadas en tiempo<\/span><\/span><\/p>\n<div>\n<pre class=\"lang:tsql decode:true \">DECLARE @t TABLE( randnum float )\r\nDECLARE @cnt INT; SET @cnt = 0\r\nWHILE @cnt &lt;=10000\r\nBEGIN\r\nSET @cnt = @cnt + 1\r\nINSERT INTO @t\r\nSELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )\r\n+ (DATEPART(ss, GETDATE()) * 1000 )\r\n+ DATEPART(ms, GETDATE()) )\r\nEND\r\nSELECT randnum, COUNT(*)\r\nFROM @t\r\nGROUP BY randnum<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>M\u00e9todo 5 :&nbsp;<span id=\"result_box\" class=\"short_text\" lang=\"es\"><span class=\"\">N\u00famero aleatorio por fila<\/span><\/span><\/p>\n<div>\n<pre class=\"lang:tsql decode:true \">---- The distribution is pretty good however there are the occasional peaks.\r\n---- If you want to change the range of values just change the 1000 to the maximum value you want.\r\n---- Use this as the source of a report server report and chart the results to see the distribution\r\nSELECT randomNumber, COUNT(1) countOfRandomNumber\r\nFROM (\r\nSELECT ABS(CAST(NEWID() AS binary(6)) %1000) + 1 randomNumber\r\nFROM sysobjects) sample\r\nGROUP BY randomNumber\r\nORDER BY randomNumber<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Documento original:<\/p>\n<p>https:\/\/blog.sqlauthority.com\/2007\/04\/29\/sql-server-random-number-generator-script-sql-query\/<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>M\u00e9todo 1 :&nbsp; Generar un n\u00famero aleatorio entre un rango &#8212;- Create the variables for the random number generation DECLARE @Random INT; DECLARE @Upper INT; DECLARE @Lower INT &nbsp; &#8212;- This will create a random number between 1 and 999 SET @Lower = 1 &#8212;- The lowest random number SET @Upper = 999 &#8212;- The [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[404],"tags":[451,450,54,53],"class_list":["post-6110","post","type-post","status-publish","format-standard","hentry","category-sql-dessarrollo","tag-aleatorio","tag-random","tag-script","tag-sql"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/posts\/6110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/comments?post=6110"}],"version-history":[{"count":2,"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/posts\/6110\/revisions"}],"predecessor-version":[{"id":6112,"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/posts\/6110\/revisions\/6112"}],"wp:attachment":[{"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/media?parent=6110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/categories?post=6110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhinfo.com.mx\/cursos\/wp-json\/wp\/v2\/tags?post=6110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}