How to obtain a list of random integers satisfying the Law of Cosine?

조회 수: 2 (최근 30일)
Hello everyone,
Can anybody help me in obtaining random integers, “a”, “b” and “c” ranging from 0 to 100, which satisfy the Law of Cosine c^2=a^2+b^2+a*b, for the fixed corresponding angle equals 120 degrees.
  댓글 수: 2
darova
darova 2019년 9월 21일
Can you show what you have tried?
John D'Errico
John D'Errico 2019년 9월 21일
Not hard, but an interesting homework assignment, but surely your homework. If you want help, then make an effort. Show how you might try to solve it, and then you might get help.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2019년 9월 21일
So I guess the triple nested for loop won't work because they need to be obtained from a random draw and not sequentially? Have you heard of the randi() function?
Do you have to find them ALL? Since to make sure you've tested them all, and you're using randi() instead of a triple-for-loop, you're going to have to ask for lots of them. Let's see, a 100x100x100 would take a million test, so taking them randomly might take a few hundred million, which is certainly within MATLAB's power. Then use a single for loop to test them all
N = 200000000; % or whatever.
r = randi(..........
for k = 1 : N
if r(1) + ....................do the test
end
  댓글 수: 12
Guillaume
Guillaume 2019년 9월 23일
Note that you can do:
[j, i, c] = find(mod(c,1)==0 & c<=100);
a = a(i)'; b = b(j);
No need for the ind2sub.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by