我写的循环 但需要得到符合要求的随机数 高手看看怎么补
declare @inti int,
@namelength int ,
@strNew char (100),
@strA char (40)
select @inti = 0,
@strA = '0123456789abcdefghigklmnopqrstuvwxyz',
@namelength = rand ()-- 需要一个1--100的随机数 做为name的长度
while (@inti < @namelength)
begin 
set @strNew ='"+ @strNew + substring(@strA, rand (),1)+ "' -- 在侯选的36个字符中随机截取一个字符,需要<=36的随机数
set @inti =@inti +1
end
还望高手指教啊

解决方案 »

  1.   

    select cast(rand()*36 as int)
      

  2.   

    我之前试过 用rand函数在一定时间内返回的随机数都一样 比如在一秒内得到的数都一样 你这个方法以前试过没?
      

  3.   

    declare @inti int,
    @namelength int ,
    @strNew char (100),
    @strA char (40)
    select @inti = 0,
    @strA = '0123456789abcdefghigklmnopqrstuvwxyz',
    @namelength = ceiling(rand(-1) * 100)-- 需要一个1--100的随机数 做为name的长度
    while (@inti < @namelength)
    begin 
    set @strNew ='"+ @strNew + substring(@strA, ceiling(rand(@inti) * 36),1)+ "' -- 在侯选的36个字符中随机截取一个字符,需要<=36的随机数
    set @inti =@inti +1
    end----------------------------------------------------------------------
    RAND
    返回 0 到1 之间的随机float 值。语法
    RAND ( [ seed ] ) 参数
    seed是给出种子值或起始值的整型表达式(tinyint、smallint 或 int)。返回类型
    float注释
    在单个查询中反复调用 RAND() 将产生相同的值。    ---------------注意这个
      

  4.   

    select cast(rand()*36 as int)
      

  5.   

    先谢谢大家的热情相助啊 帮助我看过 但没看懂 是不是说要是求36内的随机数 括号里就写36?
    但这种方法在一个较短内容易产生一样的数 特别是在循环中
    seed是给出种子值或起始值的整型表达式(tinyint、smallint 或 int)。
      

  6.   

    @namelength = rand ()-- 需要一个1--100的随机数 做为name的长度
    ---------------------------------------------------------------
    @namelength = replace(ceiling(rand()*100),'.0','')
      

  7.   

    我随机了一把 发现select cast(rand()*36 as int)的方法是对的 但应该乘以37 要不永远也得不到36的 rand是返回0--1之间的 
    @namelength = replace(ceiling(rand()*100),'.0','')是对的 不用乘以101 
    谢谢各位的热心了