"In a game of bingo the numbers 1 to 99 are drawn at random from a bag. Write a script to simulate the draw of the numbers (each number can be drawn only once), printing them ten to a line."
I've been trying to answer this. I believe I'm suppose to use a for loop and rand. Thank you!

답변 (1개)

Image Analyst
Image Analyst 2016년 11월 28일
편집: Image Analyst 2016년 11월 28일

0 개 추천

Hint, to get a list of all 99 numbers, randomized, without repeats from a list of 1-99:
numbers = randperm(99)
Then, to simulate draws of 10 numbers, you just need to get elements 1-10, 11-20, 21-30, etc. for however many draws of 10 that you want to do.

댓글 수: 5

Gregory Shoemake
Gregory Shoemake 2016년 11월 28일
That gets me started, but I the numbers that are drawn can't repeat. Thank you though, I might be able to figure it out from here.
Walter Roberson
Walter Roberson 2016년 11월 28일
No, you need to randperm() all of them at once, but then you can print them 10 per line. However, you have the problem that you have 99 numbers to be printed 10 per line, which is one number short from 10 rows of 10.
I tried vec2mat and it mostly worked but I still have a 0 at the end that I need to get rid of.
>> n = randperm(99);
>> vec2mat(n, 10)
ans =
58 65 46 84 7 68 74 89 43 27
72 59 2 95 19 49 41 21 22 16
51 82 81 56 39 63 52 48 93 57
75 1 90 40 18 97 78 32 98 47
80 8 9 73 31 10 42 87 12 77
20 71 44 17 24 5 6 38 94 23
34 3 30 36 60 55 85 54 4 76
25 13 45 64 11 53 29 83 92 26
67 28 35 86 88 69 66 96 91 15
14 62 79 37 70 33 61 99 50 0
Halp.
Walter Roberson
Walter Roberson 2016년 11월 28일
The question is not possible to solve in the form stated. You have only 99 values but you must print 10 per line. One or more lines would have to come out short, but that is not permitted with the wording you presented. You are going to have to fail the homework question, as is everyone else.
Image Analyst
Image Analyst 2016년 11월 28일
"the numbers that are drawn can't repeat." They don't , with randperm(). Perhaps you were thinking of randi() where the numbers are random and can repeat, or be missing. That's specifically why I used randperm() which essentially just shuffles/scrambles the order but each number is in there once and only once.
You can print out 10 at a time for nine draws of 10. The last draw will have only 9 in it, unless you put some back in. The homework question is valid only for up to 9 draws of 10. Any more than that you will run out of chips/numbers/balls. For example with 99 chips, you can't have a thousand draws of course. You have to assume that the number of draws will be 9 or less. It doesn't make sense otherwise.

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

카테고리

도움말 센터File Exchange에서 Board games에 대해 자세히 알아보기

질문:

2016년 11월 28일

댓글:

2016년 11월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by