Roulette with prizes algorithm - probability

I have a list of roulette items.
F.e
[
'id' => 1,
'probability' => 50 // probability in %
],
[
'id' => 2,
'probability' => 20 // probability in %
],
[
'id' => 3,
'probability' => 10 // probability in %
],
[
'id' => 4,
'probability' => 5 // probability in %
],
[
'id' => 5,
'probability' => 1 // probability in %
];
So i need to write an algorithm that will describe behaviour of the roulette due to probabilities.
I have no ideas, can someone help me with an idea?

I think if you study this you will get some Idea to write it yourself.
Fitness proportionate selection

Related

Convert array to collection laravel [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array from request like this,
[
"item_asset_id" => [1, 2]
"colour_id" => [10, 11]
"qty" => [10, 20]
"price" => [100, 300]
"tax_price" => [0, 0]
"notes" => ['Item one', item two]
]
How to convert array like that to collection with key value
i need result like this
[
['item_asset_id' => 1, 'colour_id' => 10, 'qty' => 10],
['item_asset_id' => 2, 'colour_id' => 11, 'qty' => 20],
]
btw i am using laravel framework, thank in advance
you should get each array part into separate array then set the result in result array:
$values =
[
"item_asset_id" => [1, 2],
"colour_id" => [10, 11],
"qty" => [10, 20],
"price" => [100, 300],
"tax_price" => [0, 0],
"notes" => ['Item one', 'item two'],
];
$resultArray=[];
for ($i=0;$i<2;$i++)
{
$tempArray=[];
$tempArray["item_asset_id"]=$values["item_asset_id"][$i];
$tempArray["colour_id"]=$values["colour_id"][$i];
$tempArray["qty"]=$values["qty"][$i];
$tempArray["price"]=$values["price"][$i];
$tempArray["tax_price"]=$values["tax_price"][$i];
$tempArray["notes"]=$values["notes"][$i];
$resultArray[]=$tempArray;
}
dd($resultArray);
$resultArray will have the desired result.

Summing a Hash - how can I add find the total cost for the orders in this hash

basket = {
"potato wedges" : 4.45 => 2,
"tiramisu : 4.95 => 2,
"diet coke" : 1.75 => 2
}
I want to sum this hash - the values represent the number of items, the number in the key represents the price.
hoW COULD i calculate the total price of these items\thANK YOU
Assuming you meant to write a hash like this:
basket = {
"potato wedges:4.45" => 2,
"tiramisu:4.95" => 2,
"diet coke:1.75" => 2
}
You could get the sum like this:
basket.sum {|k,v| k.split(':')[1].to_f * v}
#-> 22.3
One option is to use vector methods.
h = {
"potato wedges:4.45" => 2,
"tiramisu:4.95" => 2,
"diet coke:1.75" => 2
}
require 'matrix'
Vector[*h.keys.map { |s| s[/(?<=:).+/].to_f }].inner_product(Vector[*h.values])
#=> 22.3
See Vector::[], String#[] and Vector#inner_product. The regular expression /(?<=:).+/ reads, "match one or more characters (.+) immediately preceded by a colon", (?<=:) being a postive lookbehind.

Array#each not performing as expected with nested while loop

The method below randomly generates the letters for the 6 sides of each of 16 dice meant to be used for Boggle based off the letter_frequencies hash. When the code is run, it iterates through the first element of boggle_dice, an empty array, filling it with random letters.
However, it proceeds to use that first iteration to fill the other 15 elements in boggle_dice with the same letters it has randomly chosen for the first element.
What is causing this to happen, and how should the iterative process be altered to work as intended?
At the end of iteration, each of the keys should appear in boggle_dice the number of times equal to the value associated with it in the hash.
def create_board
letter_frequencies = {
'e' => 10,
'a' => 8,
'i' => 7,
'o' => 6,
'l' => 5,
'n' => 5,
's' => 5,
't' => 5,
'd' => 4,
'r' => 4,
'u' => 4,
'b' => 3,
'c' => 3,
'g' => 3,
'h' => 3,
'm' => 3,
'p' => 3,
'y' => 3,
'f' => 2,
'k' => 2,
'v' => 2,
'w' => 2,
'j' => 1,
'q' => 1,
'x' => 1,
'z' => 1
}
boggle_dice = Array.new(16, [])
boggle_dice.each do |die|
while die.length < 6 do
random_letter = letter_frequencies.keys.sample
letter_frequencies[random_letter] <=0 ? next : die << random_letter
letter_frequencies[random_letter] -= 1
end
end
end
That is a common beginner's mistake. It is because you did Array.new(16, []), the same array is modified repeatedly. It can be fixed by changing it to Array.new(16){[]}.

Accessing Array of Hashes

I am trying to access a value from an array of hash. An example array looks like this:
family = [
[
{ "Homer" => 1, "Marge" => 2, "Lisa" => 3, "Maggie" => 4,
"Abe" => 5, "Santa's Little Helper" => 6
}
],
[
{ "Homer" => 2, "Marge" => 4, "Lisa" => 6,
"Maggie" => 8, "Abe" => 10, "Santa's Little Helper" => 12
}
]
]
If I try to access the hash value for key "Homer" in array indexed 0 (family[0]) using the statement below and hoping to get the value 1:
family[0]["Homer"]
I get an error which says
"test.rb:4:in `[]': can't convert String into Integer (TypeError)"
Any suggestions on how one might be able to access a hash value in such an array, in a simple statement?
You should try family[0][0]["Homer"].
In your case family[0] gives you :
[{ "Homer" => 1, "Marge" => 2, "Lisa" => 3, "Maggie" => 4, "Abe" => 5,"Santa Little Helper" => 6}]
which is an array. The hash you want is inside it and can be got with family[0][0] :
{ "Homer" => 1, "Marge" => 2, "Lisa" => 3, "Maggie" => 4, "Abe" => 5,"Santa Little Helper" => 6}
So you can now use family[0][0]["Homer"] which will give you the value 1.
The array indices are always numeric values. If you get a can't convert String into Integer (TypeError) error message an exception is thrown because you are trying to access array element using a string that can't be converted to an integer.
You don't actually have an array of hashes. You have an array of arrays of hashes.
Your error occurs because you dereference your structure with [0] which gives you the first array of hashes, now you try to access the key 'homer' which doesnt exist because arrays are keyed by integers.
Here is an example of how you could see all of the values, see if you can get 'homer' on your own:
family.each do |a| # this is an array of arrays of hashes
a.each do |h| # this is an array of hashes
h.each do |k,v| # this is a hash
puts "#{k} => #{v}"
end
end
end
Output:
Homer => 1
Marge => 2
Lisa => 3
Maggie => 4
Abe => 5
Santa's Little Helper => 6
Homer => 2
Marge => 4
Lisa => 6
Maggie => 8
Abe => 10
Santa's Little Helper => 12
#Arup Rakshit is absolutely correct about how to get your value. But you should also know that you don't have an array of hashes, you have an array of arrays, and those sub-arrays contain hashes. Based on your title I'm concluding that you probably want a structure more like
family = [
{ "Homer" => 1, "Marge" => 2, "Lisa" => 3, "Maggie" => 4,
"Abe" => 5, "Santa's Little Helper" => 6
},
{ "Homer" => 2, "Marge" => 4, "Lisa" => 6,
"Maggie" => 8, "Abe" => 10, "Santa's Little Helper" => 12
}
]

Ruby way of summing up dictionary values

I have something like this:
a = [{"group_id" => 1, "student_id" => 3, "candies" => 4},
{"group_id" => 2, "student_id" => 1, "candies" => 3},
{"group_id" => 1, "student_id" => 2, "candies" => 2},
{"group_id" => 3, "student_id" => 4, "candies" => 6},
{"group_id" => 1, "student_id" => 5, "candies" => 1},
{"group_id" => 3, "student_id" => 6, "candies" => 1},
{"group_id" => 4, "student_id" => 8, "candies" => 3}]
I have three groups of students and each student gets a certain number of candies. I wish to count the total number of candies in a group. For that, I need to know the students which belong to a certain group and accumulate their candy count. I can do it using loops and initializing counts to zero:
aa = a.group_by { |a| a["group_id"] }
# =>
{
1 => [
{"group_id"=>1, "student_id"=>3, "candies"=>4},
{"group_id"=>1, "student_id"=>2, "candies"=>2},
{"group_id"=>1, "student_id"=>5, "candies"=>1}
],
2 => [{"group_id"=>2, "student_id"=>1, "candies"=>3}],
3 => [
{"group_id"=>3, "student_id"=>4, "candies"=>6},
{"group_id"=>3, "student_id"=>6, "candies"=>1}
],
4 => [{"group_id"=>4, "student_id"=>8, "candies"=>3}]
}
But I'm not able to accumulate the values within the group_id. I wonder if there are any succinct ways of representing it. How do I sum the total number of candies that are present in a group?
The first your step (grouping) is correct. After that you can use the following:
a.group_by {|g| g['group_id']}.map do |g, students|
{group_id:g, candies:students.map {|st| st['candies']}.inject(&:+)}
end
map function is often used with collections instead of loops to make some operation on each element and return modified version of the collection.
Output:
[{:group_id=>1, :candies=>7},
{:group_id=>2, :candies=>3},
{:group_id=>3, :candies=>7},
{:group_id=>4, :candies=>3}]
Adding to #StasS answer, a more direct hash way to do (with a more cryptic code) is like this:
> Hash[a.group_by{|g| g['group_id']}.map{|g,s| [g, s.inject(0){|a,b| a + b["candies"]}]}]
=> {1=>7, 2=>3, 3=>7, 4=>3}
you can unfold the line like this:
groups = a.group_by{|g| g['group_id']}
id_candies_pairs = groups.map{|g,s| [g, s.inject(0){|a,b| a + b["candies"]}]}
id_candies_hash = Hash[id_candies_pairs]
return id_candies_hash
Riffing on the answer by #StasS, you can also just build a simpler looking hash like:
totals_by_group_id = {}
a.group_by {|g| g['group_id']}.map do |g, students|
totals_by_group_id[g] = students.map {|st| st['candies']}.inject(&:+)
end
The resulting totals_by_group_id hash is:
{1=>7, 2=>3, 3=>7, 4=>3}

Resources