How should I scratch this ticket?
2 views (last 30 days)
Show older comments
One of our local car dealers is having a promotion where they give out scratch tickets. The rules are simple - scratch any six boxes on the 5 by 5 grid and if they all contain a $ sign then you win some money.
The challenge: come up with a MATLABish way to generate six unique linear indices from 1:25. Any algorithm, method, implementation is okay. The results; however, need to be repeatable when run on my system. This could be as simple as:
luckyN = @()[1 11 18 22 13 3]; %your choice lucky numbers
to anything way more complicated than I can't understand. Points for originality. Explain what you're doing so this can be sort of educational.
The winner will get an answer acceptance, and if their numbers win, something even cooler. You have until Monday June 27th at 1700 EDT. Multiple answers okay as well :)
The Results:
3 Comments
Accepted Answer
More Answers (13)
Paulo Silva
on 26 Jun 2011
function easymoneypj
figure %create a new figure
axis([1 6 1 6]) %set the axis min and max values
set(gca,'Color',[0.5 0.9 0.5]) %select the background color (light green)
set(gca,'xtick',[]) %hide the x ticks
set(gca,'ytick',[]) %hide the y ticks
set(gca,'YDir','reverse') %reverse the y direction, now it's similar to array
hold on %retain the current plot and certain axes properties
title('Good Luck') %set the title of the axis
%create the pushbutton, it's used to select another group of random numbers
pbh1 = uicontrol(gcf,'Style','pushbutton','String','New ticket',...
'Position',[10 40 60 40],...
'callback',@newcomb);
%create the textbox to show the numbers
tx = uicontrol(gcf,'Style','text','String','',...
'Position',[10 10 600 30],'FontSize',20);
newcomb %call the function to create random numbers when GUI starts
function newcomb(obj,ev) %this function create the random numbers
x = randperm(25); %returns a random permutation of the integers
%select the first 6 random integers
pick=x(1:6) %stolen code from the cyclist :) hope you don't mind
%show the random integers on the GUI textbox
set(tx,'String',num2str(pick)) %thanks
cla %delete from the current axes all graphics objects
%create the grid lines
for n=1:4
line(n+ones(6,1),1:6)
line(1:6,n+ones(6,1))
end
%draw one X on each of the six squares
for n=1:6
p=pick(n); %get each of the first 6 random integers
[x y]=ind2sub([5 5],p); %get their row and column
text(x+0.3,y+0.4,'x','FontSize',50) %put the X on the spot
end
end
end
%
6 Comments
Paulo Silva
on 27 Jun 2011
Those X just mark the spot where the $ is, you have to scratch to see the $
the cyclist
on 25 Jun 2011
rng(1); % Latest and greatest for setting the seed. (Only works on newest version of MATLAB.)
x = randperm(25);
pick=x(1:6)
1 Comment
the cyclist
on 26 Jun 2011
Sorry, I accidentally wrote "png" for seed-setting when I submitted this. Just noticed that now.
Matt Fig
on 25 Jun 2011
function [R,cnt] = winning_numbers()
% Generates 6 winning numbers on [1 25] and returns a seed to the
% twister algorithm which guarantees that the numbers are members of a
% consecutive set.
cnt = 1;
R = [];
while length(R)<6
rand('twister',cnt);
R = ceil(rand*25);
for ii = 2:6
R(ii) = ceil(rand*25);
if ~all(diff(sort(R))==1)
R = [];
break
end
end
cnt = cnt+1;
end
cnt = cnt-1;
0 Comments
Fangjun Jiang
on 26 Jun 2011
Very often, I got scratch pad from banks or local retail stores. Here is my entry. Hope your auto dealer has the same best spirit.
Ticket=repmat('$',5,5);
WinningNumber=find(Ticket=='$',6);
David Young
on 27 Jun 2011
I think that for such a hard problem, a collaborative solution is needed.
% Collect expert opinion on the topic
answers = urlread('http://www.mathworks.com/matlabcentral/answers/10272-how-should-i-scratch-this-ticket');
% Strip out everything except the body of the answers
answercells = regexp(answers, '<div class="answer_body" id="answer_\d+_body">(.*?)</div>', 'tokens');
% Merge the answers into a long string
answerstrings = cellfun(@(c) c{1}, answercells, 'UniformOutput', false);
answersString = [answerstrings{:}];
% Pick out the numbers from this string. Assume that if someone has put a
% number in their answer, they think it's lucky
lucky_numbers = str2num(regexprep(answersString,'[^0-9]+', ' '));
% Some of these lucky numbers must be for other kinds of game, so get rid
% of them
right_lucky_numbers = lucky_numbers(lucky_numbers >= 1 & lucky_numbers <= 25);
% Now choose the 6 luckiest numbers according to the votes given by the
% experts
h = hist(right_lucky_numbers, 1:25);
[~, sorted_lucky_numbers] = sort(h, 'descend');
luckiest_numbers = sorted_lucky_numbers(1:6)
0 Comments
Andrew Newell
on 26 Jun 2011
mod('I win!',25)
Here is another variant that gets to the point:
Scratch_what = mod('itches',25);
0 Comments
Paulo Silva
on 25 Jun 2011
figure
axes('xlim',[0 7],'ylim',[0 10])
nr=zeros(1,6);
for n=1:6
m=1;
h=text(n,m,'1');
while m<=8
set(h,'Position',[n,m])
nr(n)=randi([1 25]);
set(h,'String',num2str(nr(n)))
if sum(ismember(nr,nr(n)))<2 %if already selected ignore it
pause(0.2) %speed of the animation, pause 0.2 seconds by default
m=m+1;
end
end
end
nr
0 Comments
the cyclist
on 26 Jun 2011
% Inefficiently choose by tumbling through product space
luckyN = zeros(1,5);
N=2000; % Choose larger N for more suspense
while not(length(unique(luckyN))==5 && all(luckyN>=1) && all(luckyN<=25))
luckyN=round(randn(1,N)*randn(N,5)); % Remove semicolon to watch the process
end
luckyN
0 Comments
the cyclist
on 26 Jun 2011
This one gives you a choice of two lucky sequences. Both the input and output are lucky.
luckyIn = [1 2 3 4 5 6] % Fixed, after comment from Fangjun Jiang
luckyOut = sum(hankel(luckyIn))
2 Comments
Jan
on 27 Jun 2011
Shuffle(25, 'index', 6)
But let me read original rules again more carefully:
scratch any six boxes on the 5 by 5 grid and
if they all contain a $ sign
Your formulation "generate six unique linear indices from 1:25" does not match these rules exactly: There is no restriction, that I have to choose 6 different boxes. The chance that all boxes contain a $ sign is (number of $ / 25) if I choose the same box 6 times. Then one of the optimal algorithms is:
index = repmat(19, 1, 6)
Carl E. Linderholm: "Each sequence of natural number can be continued by 19."
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!