Solving Rand Function MATLAB Homework

  • Thread starter Thread starter SteliosVas
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB homework problem involving the generation of random numbers and determining how many iterations it takes for their product to be less than 1e-5. The focus includes coding errors, the nature of random numbers, and the intended outcome of the task.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the homework task and provides an initial code attempt, encountering an error related to the size of inputs for the rand function.
  • Another participant questions the need for generating random integers instead of random floating-point numbers, suggesting that the goal is to produce numbers between 0 and 1.
  • Concerns are raised about the logic of the code, particularly regarding the condition for the while loop and the use of the floor function, with requests for clarification on the variables a and b.
  • A participant emphasizes the need for specificity in defining "two random numbers," noting that certain types of random integers (like dice rolls) would not meet the criteria for the product being less than 1e-5.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate type of random numbers to generate and the logic behind the code structure. There is no consensus on the best approach to solve the problem, and the discussion remains unresolved.

Contextual Notes

Participants highlight potential misunderstandings regarding the requirements of the homework, including the definitions of random numbers and the conditions for the while loop. There are unresolved questions about the variables used in the code.

Who May Find This Useful

This discussion may be useful for students working on similar MATLAB homework involving random number generation and iteration logic, as well as those seeking clarification on coding errors and logical structuring in programming tasks.

SteliosVas
Messages
70
Reaction score
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
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?
 
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.
 
"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.
 

Similar threads

Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K