cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

24-Ruby IV

## Lotto game

Friends, I need a function that returns a vector containing n natural random non-repeating numbers (Lotto game)

1 ACCEPTED SOLUTION

Accepted Solutions
23-Emerald III
(To:ValeryOchkov)

With all numbers from 0 to n-1:

or n out of N, where N is much larger:

Success! ( with the Christmas lottery?)

Luc

Mathcad and Prime express files attached.

7 REPLIES 7
23-Emerald III
(To:ValeryOchkov)

With all numbers from 0 to n-1:

or n out of N, where N is much larger:

Success! ( with the Christmas lottery?)

Luc

Mathcad and Prime express files attached.

24-Ruby IV
(To:LucMeekes)

Thanks, but Mathcad 15

24-Ruby V
(To:ValeryOchkov)

You may use

or even better

because that way you chose numbers from 1 to n (and not from 0 to n-1) which may be more appropriate for a lottery.

A shorter variant without using "max" is

24-Ruby IV
(To:Werner_E)
24-Ruby IV
(To:ValeryOchkov)

24-Ruby IV
(To:ValeryOchkov)

SMath

24-Ruby V
(To:Werner_E)

> Why not

Hmm, thats sure a possible way. But to answer that question: Because Luc's solution using "matrix" & "runif"  is a bit shorter, faster (avoiding explicit loops) and so a bit more elegant 😄 Furthermore its ORIGIN-aware!

BTW, while I don't think the various elements of gamification here on the forum are really useful or helpful, it should still be Luc's answer that is marked as "the correct one" and not mine. Apart from the fact that I appreciate his elegant and short solution, the question did not specify the Mathcad version for which a solution is needed and his suggestions work in older versions like Mathcad 11 as well as in the current Prime versions. Just unfortunately not in MC14/15. I am not sure if this has something to do with SUC, that "matrix" in these versions does not accept built-in function, which have not explicitly only exactly two arguments.

EDIT: I see you already changed the "corrected answer".

So here is a more standard approach. Its not as elegant and short, but its faster because only one Nx1 matrix is created and the time-consuming "submatrix" is avoided.

Announcements
Top Tags