Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7766501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:26:05+00:00 2026-06-01T15:26:05+00:00

I have a function that generates normal random number matrix having normal distribution using

  • 0

I have a function that generates normal random number matrix having normal distribution using normrnd.

             values(vvvv)= normrnd(0,0.2);

The output is from round1 is:
ans =

0.0210    0.1445    0.5171   -0.1334    0.0375   -0.0165       Inf   -0.3866   -0.0878   -0.3589

The output from round 2 is:
ans =

0.0667    0.0783    0.0903   -0.0261    0.0367   -0.0952    0.1724   -0.2723       Inf       Inf

The output from round 3 is:
ans =

0.4047   -0.4517    0.4459    0.0675    0.2000   -0.3328   -0.1180   -0.0556    0.0845       Inf

the function will be repeated 20 times.

It is obvious that the function is completely random. What I seek is to add a condition.

What I need is: if any entry has a value between 0.2 and 0.3. that value will be fixed in the next rounds. Only the remaining entries will be subjected to change using the function rand.

I have found the rng(sd) which seeds the random number generator using the nonnegative integer sd so that rand, randi, and randn produce a predictable sequence of numbers.

How to set custom seed for pseudo-random number generator

but how to make several entries of the matrix only effected!!

Another problem: seems that rng is not available for matlab r2009
How to get something similar without entering in the complication of probability & statistics

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:26:06+00:00Added an answer on June 1, 2026 at 3:26 pm

    You can do this more directly than actually generating all these matrices, and it’s pretty easy to do so, by thinking about the distribution of the final output.

    The probability of a random variable distributed by N(0, .2) lying between .2 and .3 is p ~= .092.

    Call the random variable of the final output of your matrix X, where you do this n (20) times. Then either (a) X lies between .2 and .3 and you stopped early, or (b) you didn’t draw a number between .2 and .3 in the first n-1 draws and so you went with whatever you got on the nth draw.

    The probability of (b) happening is just b=(1-p)^(n-1): the independent events of drawing outside [.2, .3], which have probability 1-p, happend n-1 times. Therefore the probability of (a) is 1-b.

    If (b) happened, you just draw a number from normrnd. If (a) happened, you need the value of a normal variable, conditional on its being between .2 and .3. One way to do this is to find the cdf values for .2 and .3, draw uniformly from the range between there, and then use the inverse cdf to get back the original number.

    Code that does this:

    mu = 0;
    sigma = .2;
    upper = .3;
    lower = .2;
    n = 20;
    sz = 15;
    
    cdf_upper = normcdf(upper, mu, sigma);
    cdf_lower = normcdf(lower, mu, sigma);
    p = cdf_upper - cdf_lower;
    b = (1-p) ^ (n - 1);
    
    results = zeros(sz, sz);
    mask = rand(sz, sz) > b; % mask value 1 means case (a), 0 means case (b)
    num_a = sum(mask(:));
    
    cdf_vals = rand(num_a, 1) * p + cdf_lower;
    results(mask) = norminv(cdf_vals, mu, sigma);
    
    results(~mask) = normrnd(mu, sigma, sz^2 - num_a, 1);
    

    If you want to simulate this directly for some reason (which is going to involve a lot of wasted effort, but apparently you don’t like “the complications of statistics” — by the way, this is probability, not statistics), you can generate the first matrix and then replace only the elements that don’t fall in your desired range. For example:

    mu = 0;
    sigma = .2;
    n = 10;
    m = 10;
    num_runs = 20;
    lower = .2;
    upper = .3;
    
    result = normrnd(mu, sigma, n, m);
    for i = 1 : (num_runs - 1)
        to_replace = (result < lower) | (result > upper);
        result(to_replace) = normrnd(mu, sigma, sum(to_replace(:)), 1);
    end
    

    To demonstrate that these are the same, here’s a plots of the empirical CDFs of doing this for 1×1 matrices 100,000 times. (That is, I ran both functions 100k times and saved the results, then used cdfplot to plot values on the x axis vs portion of the obtained values that are less than that on the y axis.)

    They’re identical. (Indeed, a K-S test for identity of distribution gives a p-value of .71.) But the direct way was a bunch faster to run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have probability density function of skew normal distribution.I want to generate random number
I have a function that generates a key of 4 characters that has to
I have a framework that generates the DOM of an app completely using document.createElement
I have a function that generates a CRC check byte based on the content
I have a function that creates a 2D vector void generate(int n) { vector<
Suppose I have a function that returns a closure: sub generator { my $resource
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that accepts a class (not an instance) and, depending on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.