Convert random seed to a small value
We are trying out a modulus function to return investments in random order https://github.com/consul/consul/pull/2131 However we ran into the gotcha of having a seed value too big for the modulus function to work as expected If the seed is bigger than the investment id, the records are returned ordered by id By dividing the seed by a big number, this problem seems to get fixed
This commit is contained in:
@@ -112,7 +112,8 @@ module Budgets
|
|||||||
def set_random_seed
|
def set_random_seed
|
||||||
if params[:order] == 'random' || params[:order].blank?
|
if params[:order] == 'random' || params[:order].blank?
|
||||||
seed = params[:random_seed] || session[:random_seed] || rand(-100000..100000)
|
seed = params[:random_seed] || session[:random_seed] || rand(-100000..100000)
|
||||||
params[:random_seed] ||= Float(seed) rescue 0
|
params[:random_seed] = Float(seed) / 1000000 rescue 0
|
||||||
|
session[:random_seed] = params[:random_seed]
|
||||||
else
|
else
|
||||||
params[:random_seed] = nil
|
params[:random_seed] = nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -610,7 +610,15 @@ feature 'Budget Investments' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
expect(@first_user_investments_order).to eq(@second_user_investments_order)
|
expect(@first_user_investments_order).to eq(@second_user_investments_order)
|
||||||
|
scenario "Convert seed to a value small enough for the modulus function to return investments in random order", :focus do
|
||||||
|
12.times { |i| create(:budget_investment, heading: heading, id: i) }
|
||||||
|
|
||||||
|
visit budget_investments_path(budget, heading_id: heading.id, random_seed: '12')
|
||||||
|
|
||||||
|
order = investments_order
|
||||||
|
orderd_by_id = Budget::Investment.order(:id).limit(10).pluck(:title)
|
||||||
|
|
||||||
|
expect(order).to_not eq(orderd_by_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def investments_order
|
def investments_order
|
||||||
|
|||||||
Reference in New Issue
Block a user