Generating random 20 number between 0.25 to 2.8 and counter odd numbers in 'array'?

Generating random 20 number between 0.25 to 2.8 and counter odd numbers in 'array'?
I can't do this :(
it's not working:
g=randi([0.25 2.8],1,20)
numberOfOddNumbers = sum(rem(g, 2))

 Accepted Answer

a = .25;
b = 2.8;
r = (b-a).*rand(1,20) + a; % note that your creating floating numbers

7 Comments

is not working - its not counter :(
its show me the number...
you are creating floating numbers between .25 and 2.8
what are the odd numbers between them?
can you explain?
if i write this:
a = .25;
b = 2.8;
r = (b-a).*rand(1,10) + a; % note that your creating floating numbers
numberOfOddNumbers = rem(r, 2);
r
numberOfOddNumbers
its show me:
r =
0.6504 1.2021 2.3113 2.5688 0.6598 2.1318 1.6670 0.7290 2.0095 1.0145
numberOfOddNumbers =
0.6504 1.2021 0.3113 0.5688 0.6598 0.1318 1.6670 0.7290 0.0095 1.0145
numberOfOddNumbers - is not a counter odd.
First of all I want to present one digit after the dot, for example:
1.2, 0.9, 1.3....
odd number 0.9, 1.1, 1.3, 1.5...
How do I write conter this numbers?
By the standard definitions, there's only one odd number and one even number between 0.25 and 2.8. Most numbers in that range are neither even nor odd. It's highly unlikely that you'll generate exactly 1 or exactly 2 with your call to rand. If that's what you're trying to count, setting numberOfOddNumbers to 0 will be right almost all the time.
If you have a different definition for "odd number" then tell us what definition you're using and we may be able to help you determine how to count how many of your randomly generated numbers satisfy your definition. For instance, if you want to know how many of your numbers round to an odd number, that's easy to compute.
You're right, I need to know if the three digit after the dot is odd and then the sum.
The majority of random numbers between 0.25 and 2.8 don't have just three digits after the dot. In fact an infinite number of them have an infinite numbers of digits after the dot.
In the real of computers, among the 15,312,238,733,059,687 numbers than can be generated there's only 2552 numbers with 3 or less digits after the dot. Most of these 2552 numbers can't even be stored exactly as double.

Sign in to comment.

More Answers (1)

It seems to me what you're actually asking is to generate integer numbers between 250 and 2800. These numbers can easily be tested to see whether they're odd or even.
You can then pretend that these integers are divided by 1000 to get numbers between 0.25 and and 2.8 with at most 3 digits. Note that actually performing the division won't get you numbers that are actually at most 3 digits due to the way numbers are stored on a computer. For example, the result 400 / 1000 is not stored as 0.4, it is stored exactly as 0.40000000000000002220446049250313080847263336181640625.
numbers = randi([250 2800], 1, 20); %numbers are implicitly divided by 1000
sumodd = sum(mod(numbers, 2)); %as explained by Steven Lord, that's a weird definition of even/odd

3 Comments

1. I need to build a series of 10 random numbers between 0.25 and 2.8.
2. Then I have to pad the numbers at 0 from each side. (For example - 0 1.3 0 2.5 0 0.24)
3. I need to sum the amount of odd numbers from the series (For example - 1.3 2.5)...its 2
it so difficult :(
1. See Madhan's answer
2. Easy enough
3. Now, we're back to talking about even/odd real numbers, which as explained by Steven Lord doesn't make any sense. If you're talking about fraction of 1000 numbers whose numerator is even or odd, then see my answer.
Nothing is particularly difficult as long as you use the same math as everybody else.
Okay, is it possible to check that the second digit after the point is odd?
for example:
2.45 - odd
1.38 - even
1.2 - even
0.89 - odd

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!