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 9216833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:30:02+00:00 2026-06-18T02:30:02+00:00

I am trying to generate an array of uniformly distributed floating point values in

  • 0

I am trying to generate an array of uniformly distributed floating point values in single precision in MATLAB.

I want to generate all numbers in the range +/- (2-2^-23)*2^127 which represents the range of possible 32-bit floating point numbers based on the IEEE-754 standard. The problem is that only large magnitude numbers are being generated, and I want small magnitude numbers (near and including 0) to be included as well. This is seen if we take the absolute value of all numbers generated and then find the smallest (I have copied the output below the code).

So far I have this code in MATLAB:

numtogenerate = 20000;

% Preallocate for speed
generatednumber(numtogenerate) = 0;

for i = 1:numtogenerate
   generatednumber(i) =  rand*(2-2^-23)*2^127*2 - 2^127*(2-2^-23); 
end

minimum = min(generatednumber)
smallest = min(abs(generatednumber))
maximum = max(generatednumber)

hist(generatednumber)

Here is the output:

minimum =

 -3.4026e+038


smallest =

  8.4046e+033


maximum =

  3.4027e+038
  • 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-18T02:30:03+00:00Added an answer on June 18, 2026 at 2:30 am

    (Why in the name of god and little green apples would you do this in a loop?)

    My point is, do it using the capabilities of MATLAB. Learn to use vectors and arrays. Apply operations to an entire array of numbers. This is how a tool like MATLAB shines. Until you do, you might as well be using a lower level language, but without the speed benefits gained from using that lower level tool.

    Ok, rant over, so how do we attack this problem?

    Generate each number using THREE different random values.

    1. A random sign
    2. A random exponent
    3. A random mantissa

    Do it all using vector ops.

    numtogenerate = 20000;
    
    % the sign
    S = (rand(numtogenerate,1) < 0.5)*2 - 1;
    
    % The exponent
    E = floor(rand(numtogenerate,1)*256) - 128;
    
    % The mantissa
    M = rand(numtogenerate,1)*2 - 2^-23;
    
    % bring it all together
    R = S.*M.*2.^E;
    

    Do they cover the entire range? It seems so.

    min(abs(R))
    ans =
          7.44202895026248e-41
    
    max(R)
    ans =
          3.17337113940593e+38
    
    min(R)
    ans =
          -3.3810631675676e+38
    

    Assuming I got the ranges correct for each part, this should essentially generate every possible value in the desired range.

    By the way, these are NOT uniformly distributed numbers, at least NOT in the way that term is usually applied in mathematics.

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

Sidebar

Related Questions

I am trying to generate an array with N integer values between 0 and
I am trying to generate an array from some starting values using this formula
I am trying to generate a array containing all two letter word combinations. What
I'm trying to generate an array of random numbers from 0-n then shuffle (but
I'm trying to generate an array of integers contains randoms adding up to a
I'm trying to generate a set of check-boxes from an object array. I'm aiming
I am trying to generate a drop-down list with values from a MySQL table
i am trying to generate dynamic array as foreach($this->data['Carcase'] as $key=> $value) { if(!empty($value))
Trying to generate db driven menu which based on parent->child structure. All root menu
I am trying to clone a table row and generate an array with id

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.