set diffrenet rarity for random values in matlab

One way to set different probabilities for random values in MATLAB is to use the discrete function. This function generates random values according to a probability distribution specified by a vector of weights.

For example, let's say we want to generate random integers between 1 and 5 with different probabilities: 10% for 1, 20% for 2, 30% for 3, 25% for 4, and 15% for 5.

We can do this using the discrete function as follows:

main.m
weights = [0.1, 0.2, 0.3, 0.25, 0.15]; % probabilities for each value
values = 1:5; % possible values
n = 1000; % number of random values to generate

% generate random values
r = discrete(weights, values, n);

% count occurrence of each value
counts = histcounts(r, [values, 6]);

% display results
bar(values, counts/n);
xlabel('Value');
ylabel('Probability');
title('Random values with different probabilities');
416 chars
16 lines

This will generate 1000 random integers between 1 and 5 with the specified probabilities, and display a bar chart showing the frequency of each value.

related categories

gistlibby LogSnag