Solving Rand Function MATLAB Homework

In summary: On the other hand, random numbers between 0 and 1 can be very small, but they cannot be used to form a product unless they are very small together. So there has to be some kind of distribution, like a normal distribution. Also, there has to be some kind of bound or limit, the numbers cannot be infinitely large or small. We could use a log-normal distribution, which is never a negative number. But I don't want to guess, you have to tell me exactly what you want to do.
  • #1
SteliosVas
70
0

Homework Statement



The number of iterations it takes for the product of two random numbers to produce a number less than 1e-5.

Homework Equations

The Attempt at a Solution

while z < 1e-05
count1 = count + 1;
x = rand(1,1); %rand 1x1 matrix
z = floor(a + (b-a+1) * rand(b,1));
if z < 1e-05
break
end

endNow I keep getting an error saying:
Error using rand
Size inputs must be scalar.

Why is this?EDIT:

while 2
% randomIntegers < 1e-05
count1 = count1 +1;
randomIntegers = randi([-10,10],[20,1]);

if randomIntergars > 1e-05
break
end

end

I tried this and it's still not working
 
Last edited:
Physics news on Phys.org
  • #2
Why do you want to produce random integers? I guess the random numbers are supposed to be floats between 0 and 1.
SteliosVas said:
while z < 1e-05
That looks like the opposite of what you want to do.
SteliosVas said:
z = floor(a + (b-a+1) * rand(b,1));
What does that line do? What are a and b? Why floor()?

rand(1,1) returns a 1x1-matrix of random numbers, which is just a random number. Anyway, the inputs have to be scalar, apparently your b is not a scalar - what is b?
 
  • #3
Essentially what I want to do is generate two random numbers and their product being less than 1e-05.

That is all in trying to do.
 
  • #4
"Two random numbers" is too unspecific. Dice rolls give random integers between 1 and 6 - they are random numbers, but clearly not those you want here because their product is never smaller than 1.
 

1. How do I solve a rand function in MATLAB?

The rand function in MATLAB generates a random number between 0 and 1. To solve for a specific number, you need to use the rand function in combination with other mathematical operations such as multiplication or addition. For example, to generate a random number between 1 and 10, you can use the expression rand*10+1.

2. Can I set a specific range for the rand function in MATLAB?

Yes, you can set a specific range for the rand function in MATLAB by using the syntax rand(M,N), where M and N are the dimensions of the matrix you want to generate. For example, rand(3,4) will generate a 3x4 matrix with random numbers between 0 and 1.

3. How can I generate a random integer using the rand function in MATLAB?

The rand function in MATLAB only generates random numbers between 0 and 1. To generate a random integer, you can use the ceil function in combination with the rand function. For example, ceil(rand*10) will generate a random integer between 1 and 10.

4. Is it possible to generate a specific distribution of random numbers using the rand function in MATLAB?

Yes, you can generate a specific distribution of random numbers using the rand function in MATLAB by using the syntax rand(1,N,'distribution'), where N is the number of random numbers you want to generate and 'distribution' can be replaced with 'normal' for a normal distribution or 'uniform' for a uniform distribution.

5. How can I seed the rand function in MATLAB for reproducible results?

To seed the rand function in MATLAB, you can use the syntax rng(seed), where seed is a positive integer. This will set the seed for the random number generator, allowing you to reproduce the same set of random numbers every time you run the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
827
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
811
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
956
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top