How can I create a random array between a range of two other arrays? - random

I need to generate an array of random 20 bytes between a given range of arrays. Since arrays are comparable in Rust, this works:
let low = [0u8; 20];
let high = [2u8; 20];
assert_eq!(true, low < high);
assert_eq!(false, low > high);
assert_eq!(true, low == [0u8; 20]);
For these bounds:
let low: [u8; 20] = [98, 0, 1, 0, 2, 6, 99, 3, 0, 5, 23, 3, 5, 6, 11, 8, 0, 2, 0, 17];
let high: [u8; 20] = [99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
These would be a valid result:
[98, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
These are not:
[98, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]
I want to do something like:
use rand::prelude::*;
fn main() {
let low = [0u8; 20];
let high = [2u8; 20];
let value = rand::thread_rng().gen_range(low, high);
println!("{:?}", value);
}
but I get following error:
error[E0277]: the trait bound `[u8; 20]: rand::distributions::uniform::SampleUniform` is not satisfied
--> src\main.rs:6:36
|
6 | let value = rand::thread_rng().gen_range(low, high);
| ^^^^^^^^^ the trait `rand::distributions::uniform::SampleUniform` is not implemented for `[u8; 20]`
I tried implementing SampleUniform and UniformSampler without much success. Is there a simple way to implement this?

If you want to treat the byte arrays as big integers, use the
num-bigint crate with the rand feature enabled:
use bigint::{ToBigInt, RandBigInt};
let low = -10000.to_bigint().unwrap();
let high = 10000.to_bigint().unwrap();
let b = rng.gen_bigint_range(&low, &high);
You could also use unsigned integers instead of signed. There are methods to convert to and from big endian byte arrays:
from_bytes_be
to_bytes_be
See also:
How do I generate a random num::BigUint?

Related

Rust performance issue - High complexity code

I bought RUST by its performance so I decided to translate one project where performance matters a lot, from JAVA 11 to Rust.
The thing is the Version written in JAVA performance pretty much better more than 3x in single thread, +10X in multi thread
For context purpose: The most complex code is a function that trys to find an assigment between 2 sets, imagine that you have houses and stores, the stores have a fixed capacity and houses have necesities, you want to find the best assignment to walk less.
With all this in mind, I guess that the problem is how I use the variables, maybe clone() is called too much automatically, maybe reference access cause some unknown behavior.
Any upgrade that reduce while loop time will be great because it iterate over 5000 times. Sorry for the long code but I think everything is relevant in this case. You can't copy and paste this code if you want I can send you the git project link.
PD: I'm running with cargo run --release
pub fn evaluate(elem: &Element) -> EvaluatedElement {
let p1 = properties::get_cast::<f64>("p1");
let p2 = properties::get_cast::<usize>("p2");
let p3 = properties::get_cast::<usize>("p3");
let p4 = properties::get_cast::<f64>("p4");
let p5 = properties::get_array::<usize>("p5");
let mut kinds1 = kind1::get_map(); //almost 300 elements
let kinds2 = = kind2::get_map(); //almost 300 elements
let usables = elem.usables();
for (i, &a) in usables.iter().enumerate() {
if !a {
&kinds1.remove(&(i + 1));
}
}
let mut assignations = HashMap::new();
for k in (1..=p2).rev() {
let mut kinds2_sub = HashMap::with_capacity((&kinds2).len());
for (_, p) in kinds2.iter() {
if p.val1[k - 1] == 0 {
continue;
}
&kinds2_sub.insert(p.id, Kind2Sub {
parent: p.clone(),
val2: p.val1[k - 1],
val3: std::f64::MAX,
kind1_id: std::usize::MAX,
});
}
let mut opt_kind1_id: Option<usize> = Option::None;
while !&kinds2_sub.is_empty() {//arround 5500 times loop
for mut l in kinds2_sub.values_mut() {
match opt_kind1_id {
None => (),
Some(id) => if !l.kind1_id == id { continue; },
}
l.val3 = std::f64::MAX;
l.kind1_id = std::usize::MAX;
for b in kinds1.values_mut() {
let dist_b_l = calc_dist(b.id, l.id);
if dist_b_l > p4
|| (p1 as usize).min(l.val2) > p4 + b.val3
|| b.val2 < k
|| (l.val2 < (2 * p4) && (b.val3 as i16 - l.val2 as i16) < 0)
{ continue; }
let tmp = dist_b_l * p1.min(l.val2 as f64);
if l.val3 > tmp {
l.val3 = tmp;
l.kind1_id = b.id;
}
}
}
let lc = kinds2_sub.values_mut().min_by(|x, y| x.val3.partial_cmp(&y.val3).unwrap()).unwrap();
let obc = kinds1.get_mut(&lc.kind1_id);
let bc = obc.unwrap_or_else(|| {
panic!("No assignation able")
});
let b_c_id = (*bc).id;
let l_c_id = (*lc).id;
let time = if lc.val2 < (2usize * p1 as usize) { lc.val2 } else { p1 as usize };
let val = (*bc).val3 as i16 - time as i16;
let assignation = Assignation { kind1_id: (*bc).id, kind2_id: lc.id, val3: k, val4: 0 };
let assignation_id = assignation.id();//id() = fn concatenate first 3 values
if !assignations.contains_key(&assignation_id) {
assignations.insert(assignation.id(), assignation);
}
let mut assignation = assignations.get_mut(&assignation_id).unwrap_or_else(|| panic!("Assignation not found {}", assignation_id));
if val >= 0 {
assignation.val4 += time;
lc.val2 -= time;
(*bc).val3 -= time;
} else {
assignation.val4 += (*bc).val3;
lc.val2 -= (*bc).val3;
(*bc).val3 = 0;
}
if (*bc).val3 < p4 {
&kinds1.remove(&b_c_id);
}
if lc.val2 == 0 {
&kinds2_sub.remove(&l_c_id);
}
opt_kind1_id = Some(b_c_id);
}
}
let assignations_values = assignations.iter().map(|(_, v)| v.clone()).collect();
EvaluatedElement::evaluation(assignations_values)
}
Now I have a 4X increase.
Step Value Time Used Stores
RUST -> BI 90 2672540 28057 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 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 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0]
The "bug"
match opt_kind1_id {
None => (),
Some(id) => if !l.kind1_id == id { continue; },
}
The fix
if let Some(id) = opt_kind1_id {
if l.kind1_id != id {
continue;
}
}
This continue skips 90% of finding a new value

How to remove elements from a 3D array in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to remove first element from the every 1D array in the 1st, 2nd and 5th column.
The 3D array looks like
1st col 2nd col 3rd col 4th col 5th col
[
[[0, 0, 0, 2, 3, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0]]
]
I am not sure what is best way to do this in ruby.
If I understand your question correctly, you can do the following, where arr is your array:
arr_cpy = Marshal.load(Marshal.dump(arr))
[0,1,4].each { |i| arr_cpy[i] = arr_cpy[i].transpose[1..-1].transpose }
arr_cpy
I've used Marshal#dump and Marshal#load to make a "deep copy" of arr, so that arr will not be altered ("mutated"). If you wish to mutate arr, simply skip that step and change arr_cpy to arr in the second line.
Try this:
array.map do |row_of_column_arrays|
row_of_column_arrays.each_with_index.map do |column_arr, index|
[0, 1, 4].include?(index) ? column_arr[1..-1] : column_arr
end
end
Bottom-up, your array is made out of the following dimensions:
(1) Cell
(2) Column Array (e.g. [0,0,0,2,3,0,0,5])
(3) Row of Column Array (e.g. the first row in your question)
(4) Array of Rows of Column Array (this is the value residing in array.
So, the first map is applied on #4 and goes over each Row of Column Arrays (3). For each of this we run the second map method, which goes over each Column Array (2). We simply return from that block that particular Column Array without the first element, if we are dealing with indices 0, 1, and 4 (corresponding to 1st, 2nd and 5th columns):
[0,0,0,2,3,0,0,5][1..-1]
# => [0,0,2,3,0,0,5]

How to push a value into a multi-dimensional array (RUBY)

I'm looking to push the value 1 into an array of all 0's at a random index point.
The format of the array is a 10 by 10 lot of 0's arranged into a square table. I want to generate a random input point for this, and change that value to 1.
You can try as below :-
# First create an array of array
array = Array.new(10) { Array.new(10) { 0 } }
# method to get the random index.
def random_index(start_point = 0, end_point)
(start_point..end_point).to_a.sample
end
# First find out the any random inner array
inner_array = array[random_index(0, array.size-1)]
# Then get the any random index from the inner array and update the value.
inner_array[random_index(0, array.size-1)] = 1
array
# => [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
To answer the OP's comment :-
def get_index_of_item_from_inner_array(array, item)
first_inner_array_contains_item = array.find { |in_ary| in_ary.include? item }
(0..first_inner_array_contains_item.size - 1).find { |ind| first_inner_array_contains_item[ind] == item }
end
get_index_of_item_from_inner_array(array, 1) # => 2
One way:
arr = Array.new(10) { Array.new(10) { 0 } }
row, col = rand(100).divmod(10)
arr[row][col] = 1
row #=> 7
col #=> 6
arr
#=> [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Opencv Filter Performance

I am trying to process a real time video which consists of frames with 2048*2000 resolution.
When i profile my code, i see that the bottleneck is the below code :
filter_gauss->apply(src, proc_img);
cv::filter2D(proc_img, frame_seq[0], -1, kernel[0]);
cv::filter2D(proc_img, frame_seq[1], -1, kernel[1]);
cv::filter2D(proc_img, frame_seq[2], -1, kernel[2]);
cv::filter2D(proc_img, frame_seq[3], -1, kernel[3]);
cv::threshold(frame_seq[0], frame_seq[0], threshold, 255, cv::THRESH_BINARY);
cv::threshold(frame_seq[1], frame_seq[1], threshold, 255, cv::THRESH_BINARY);
cv::threshold(frame_seq[2], frame_seq[2], threshold, 255, cv::THRESH_BINARY);
cv::threshold(frame_seq[3], frame_seq[3], threshold, 255, cv::THRESH_BINARY);
proc_img = frame_seq[0] | frame_seq[1] | frame_seq[2] | frame_seq[3];
Is there any possible ways to improve this part of code in terms of performance ?
The kernels used above are like these :
kernel[0]
-1, -1, 4, -1, -1
kernel[1]
-1, -1, 4, -1, -1
kernel[2]
-1, 0, 0, 0, 0, 0, 0,
0, -1, 0, 0, 0, 0, 0,
0, 0, -1, 0, 0, 0, 0,
0, 0, 0, 6 , 0, 0, 0,
0, 0, 0, 0, -1, 0, 0,
0, 0, 0, 0, 0, -1, 0,
0, 0, 0, 0, 0, 0, -1
kernel[3]
0, 0, 0, 0, 0, 0, -1,
0, 0, 0, 0, 0, -1, 0,
0, 0, 0, 0, -1, 0, 0,
0, 0, 0, 6 , 0, 0, 0,
0, 0, -1, 0, 0, 0, 0,
0, -1, 0, 0, 0, 0, 0,
-1, 0, 0, 0, 0, 0, 0
filter_gauss
cv::Mat gaussian_kernel = cv::getGaussianKernel(7, 5.0);
cv::Ptr<cv::FilterEngine> filter_gauss = cv::createSeparableLinearFilter(CV_8UC1, CV_8UC1, gaussian_kernel, gaussian_kernel);

It is possible to get a binary array from a BinData::Record instance?

I'm using Ruby gem Bindata, using the following code:
require 'bindata'
class Rectangle < BinData::Record
endian :little
uint16 :len
string :name, :read_length => :len
uint32 :width
uint32 :height
end
rectangle = rectangle.new
rectangle.len = 12
It is possible to get from rectangle instance an array like [0, 1, 1, 0, 0, ...] with the binary representation of all the fields inside the object?
BinData::Base#to_binary_s returns "the string representation of this data object":
rectangle.to_binary_s
#=> "\f\x00\x00\x00\x00\x00\x00\x00\x00\x00"
This can be converted to a bit string via String#unpack:
rectangle.to_binary_s.unpack('b*')
#=> ["00110000000000000000000000000000000000000000000000000000000000000000000000000000"]
Or to a bit array via:
rectangle.to_binary_s.unpack('b*')[0].chars.map(&:to_i)
#=> [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Resources