Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby//范围之间的随机数,确保与其他现有存储的唯一性_Ruby_Random_Integer - Fatal编程技术网

Ruby//范围之间的随机数,确保与其他现有存储的唯一性

Ruby//范围之间的随机数,确保与其他现有存储的唯一性,ruby,random,integer,Ruby,Random,Integer,当前正在尝试生成特定范围内的随机数; 并确保它对其他存储的记录是唯一的 使用Mysql。可以像id一样递增;但不可能 目前以“昂贵”的方式测试其他现有记录; 但我很确定会有1/2行干净的代码可以使用 目前正在使用: test = 0 Order.all.each do |ord| test = (0..899999).to_a.sample.to_s.rjust(6, '0') if Order.find_by_number(test).nil? then break end

当前正在尝试生成特定范围内的随机数; 并确保它对其他存储的记录是唯一的

使用Mysql。可以像id一样递增;但不可能

目前以“昂贵”的方式测试其他现有记录; 但我很确定会有1/2行干净的代码可以使用

目前正在使用:

test = 0
Order.all.each do |ord|
  test = (0..899999).to_a.sample.to_s.rjust(6, '0')
  if Order.find_by_number(test).nil? then
    break
  end
end
return test

感谢您的帮助

我想,您可以做如下操作:

def uniq_num_add(arr)
 loop do
   rndm = rand(1..15)  # I took this range as an example
    # random number will be added to the array, when the number will 
    # not be present
    break arr<< "%02d" % rndm unless arr.include?(rndm)
  end  
end  

array = []

3.times do
 uniq_num_add(array)
end  

array # => ["02", "15", "04"]

我想,你可以做如下事情:

def uniq_num_add(arr)
 loop do
   rndm = rand(1..15)  # I took this range as an example
    # random number will be added to the array, when the number will 
    # not be present
    break arr<< "%02d" % rndm unless arr.include?(rndm)
  end  
end  

array = []

3.times do
 uniq_num_add(array)
end  

array # => ["02", "15", "04"]

我会这样做:

# gets all existing IDs
existing_ids = Order.all.select(:number).map(&:number).map(&:to_i)

# removes them from the acceptable range
available_numbers = (0..899999).to_a - existing_ids

# choose one (which is not in the DB)
available_numbers.sample.to_s.rjust(6, '0')

我会这样做:

# gets all existing IDs
existing_ids = Order.all.select(:number).map(&:number).map(&:to_i)

# removes them from the acceptable range
available_numbers = (0..899999).to_a - existing_ids

# choose one (which is not in the DB)
available_numbers.sample.to_s.rjust(6, '0')

这是我的单线解决方案。它也是自调用以来速度最快的一个。从订单表中提取号码。select为每个非常昂贵且不必要的记录实例化一个Order对象。它还避免了使用.map再次迭代每个对象以获取数字字段。如果我们使用CAST(在本例中为CAST)将第二个.map转换为数据库中的数值,那么我们也可以避免使用第二个.map

(Array(0...899999) - Order.pluck("CAST('number' AS UNSIGNED)")).sample.to_s.rjust(6, '0')

这是我的单线解决方案。它也是自调用以来速度最快的一个。从订单表中提取号码。select为每个非常昂贵且不必要的记录实例化一个Order对象。它还避免了使用.map再次迭代每个对象以获取数字字段。如果我们使用CAST(在本例中为CAST)将第二个.map转换为数据库中的数值,那么我们也可以避免使用第二个.map

(Array(0...899999) - Order.pluck("CAST('number' AS UNSIGNED)")).sample.to_s.rjust(6, '0')


请发布到目前为止您编写的代码。@toro2k用示例编辑。比如说,我不确定如何进行快速、干净的测试,如果已经调用了随机“生成器”,则如何调用它existing@Ben,我已经把我的答案贴出来了。它考虑到了性能,而且由于您将经常使用它,我想,您应该尝试一下;清晰易懂,请发布您目前编写的代码。@toro2k用示例编辑。比如说,我不确定如何进行快速、干净的测试,如果已经调用了随机“生成器”,则如何调用它existing@Ben,我已经把我的答案贴出来了。它考虑到了性能,而且由于您将经常使用它,我想,您应该尝试一下;清晰易懂的可用数字不能编辑这一个字母。但是是的,谢谢@Uri,您的解决方案工作正常,但此代码效率很低。all.select:number.map&:number.map&:to\i有几个原因。available\u numberS无法编辑这一个字母。但是是的,谢谢@Uri,您的解决方案工作正常,但这段代码效率很低。出于以下几个原因,all.select:number.map&:number.map&:to\i。无符号强制转换用于什么?我会去mysql文档进行检查,但您似乎非常清楚,在mysql中,将varchar转换为一种整数值是最安全的方法,因为order.number在varchar中。检查一下,您是指在该功能范围内使用的最安全的还是全局使用的?我的“数字”字段是一个整数。。。但是你会说整数字段不能是零填充的,比如000018?我必须检查一下我的侧边,我们认为您的“number”字段是一个varchar,因为在您的示例中,您将它转换为string:to_.s.rjust6,“0”。如果它是一个整数,你可以删除强制转换。是的,当然,我必须假设不是这方面的专家,也不是那么准确。非常感谢你非常清晰的回答,帮了我很多忙。无签名演员是用来干什么的?我会去mysql文档进行检查,但您似乎非常清楚,在mysql中,将varchar转换为一种整数值是最安全的方法,因为order.number在varchar中。检查一下,您是指在该功能范围内使用的最安全的还是全局使用的?我的“数字”字段是一个整数。。。但是你会说整数字段不能是零填充的,比如000018?我必须检查一下我的侧边,我们认为您的“number”字段是一个varchar,因为在您的示例中,您将它转换为string:to_.s.rjust6,“0”。如果它是一个整数,你可以删除强制转换。是的,当然,我必须假设不是这方面的专家,也不是那么准确。非常感谢你非常清晰的回答,帮了我很大的忙