How to create a random number as i asked below.

Hello everyone here is my question;
i want to create a random number with 4 number of digits but it needs some speciality.I mean 1 number shouldnt repeat again.For example
2222 is wrong 1234 is correct.
it supposed to be between 1000 and 9999 and user should guess the number for example again lets say our number is; 1234 if i guess 1235 program should output +++- or if i wrote 2234 program should -++-.
Thanks for helps.

Respuestas (2)

dpb
dpb el 3 de Jun. de 2015
doc randperm

4 comentarios

randperm(k,n) does not work on my pc it gives error only accepts randperm(k)
x = randperm(k);
x = x(1:n);
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 3 de Jun. de 2015
Editada: Salaheddin Hosseinzadeh el 3 de Jun. de 2015
I like James solution, it's easy. Just keep K as 9 and be aware that you probably wont have a 0 in your digits. You also don't want any repeated digits so, your smallest value will be 1234, up to 9876.
If you want the 0 possibility but not begin with 0, here are two solutions:
% Only one randperm call but solution not quite uniform distribution
x = randperm(10) - 1;
if( x(1) )
x = x(1:4);
else
x = x(2:5);
end
% Slightly more randperm calls on average, solution is uniform
x = 0;
while( x(1) == 0 )
x = randperm(10) - 1;
end
x = x(1:4);

Iniciar sesión para comentar.

serkan yassikaya
serkan yassikaya el 3 de Jun. de 2015

0 votos

now i have this problem when im determining the equality of digits system gives answer with logical like; 0000 or 0101 but it should bt ++++ and ---- 's how may i change this setup.
Btw thanks a lot for your helps.

4 comentarios

James Tursa
James Tursa el 3 de Jun. de 2015
Editada: James Tursa el 3 de Jun. de 2015
Assuming 0 means incorrect (-) and 1 means correct (+), e.g.,
% Test data
g = logical([0 1 0 1]);
mp= '-+';
s = mp(g+1);
Thanks so much made my day! great persons you are.

display('Tahmininizi yaptığınızda eğer doğru ise + yanlıs ise - alacaksınız!')

display('Eğer çok sıkılırsanız veya bulamaz ve sinirlenirseniz lütfen "game over" yazınız:)')

x = 0; while( x(1) == 0 ); x = randperm(10) - 1; end x = x(1:4);

a = x(1:1); b = x(2:2); c = x(3:3); d = x(4:4);

A = [a, b, c, d,]

prompt = 'Bir tahminde bulunun lütfen.';

H = input(prompt);

e = H/1000; e = floor(e); %binler basamağı buradan çıkar. r = H - e * 1000; r = r / 100; r = floor(r); % yüzler basamağı buradan çıkar. t = H-e*1000-r*100; t = t / 10; t = floor(t); y = H-e*1000-r*100-t*10; y = y / 1 ; y = floor(y); %onlar basamağı burdan çıkar.

J = [e,r,t,y,]

g = logical([0 1 0 1]); mp= '-+'; s = mp(g+1);

A == J

still gives answers logical..

Iniciar sesión para comentar.

Preguntada:

el 3 de Jun. de 2015

Comentada:

el 3 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by