Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning, all. I am a newb when it comes to this stuff. Thanks in advance for any help.

I need the following code to deliver 3 unique numbers that are greater than 0 and less than 13.


I keep running into problems with returning a 0 or two answers being the same.

What I have tried:

JavaScript
var numb = Math.floor(Math.random()* 13;
var numb2 = Math.floor(Math.random() * 13;
var numb3 = Math.floor(Math.random() * 13;

if (numb != 0) {
    var index =  numb;
} else {
    var index = 1;
}


if (numb2 != 0 && numb2 != numb) {
    var index2 = numb2;
} else  {
    var index2 = numb + 1;
    
}

if (numb3 != 0 && numb3 != numb1 && numb3 != numb2) {
    var index3 = numb3;
} else if (numb2 + 1 == numb) {
    var index3 = numb2 + 2
    else {
   	index3 = numb2 + 1 	
    
    };
}
document.write(index);

document.write(index2);

document.write(index3);
Posted
Updated 9-Mar-16 4:33am
v2
Comments
CHill60 9-Mar-16 9:36am    
Rather than just checking each number once, use a While loop to keep checking - if any of the numbers are the same or 0 just regenerate them all
random_logic 9-Mar-16 9:44am    
Thank you. I am so new at this i'm not sure how to do that.
Sinisa Hajnal 9-Mar-16 9:58am    
Range is too small to reliably have different numbers. You have to check if you already have the number and randomize again.
random_logic 9-Mar-16 10:00am    
I think I understand what you are saying, however I'm not sure how to write the code to make it do that. What would something like this look like?
Sinisa Hajnal 9-Mar-16 9:58am    
As for returning zero, you need to add +1 at the end so you get range of 1 to whatever

Math.random() returns a random number between 0 (inclusive) and 1 (exclusive), with a bit of trick, you can have
Math.random()*12+1

that will return a random number between 1 (inclusive) and 13 (exclusive).
In this way, there is no need to check for the occurrence of 0.
Since you are re-using the same code to generate multiple random numbers, put this code in a function for better code re-use, e.g.
JavaScript
function getRndNumb(){
  return Math.floor(Math.random()*12+1);
}

Lastly, to repeatedly re-generate random numbers when there is duplication, use a loop, in this case do...while is more appropriate, e.g.
do {
   numb1 = getRndNumb();
   numb2 = getRndNumb();
   numb3 = getRndNumb();
} while (numb1 == numb2 || numb1==numb3 || numb2==numb3)

I shall leave it to you to put together these pieces.
 
Share this answer
 
v2
Comments
random_logic 9-Mar-16 13:10pm    
Thank you Peter Leow! It worked like a charm! How would something like this work if I wanted to have 13 unique numbers?
Rather than just checking the numbers once, check them in a while loop
JavaScript while Loop[^]

e.g. you could do something like this (Note - untested)
JavaScript
while (numb1 == numb2 || numb2 == numb3 || numb1 == numb3 || numb1 == 0 || numb2 == 0 || numb3 == 0){
    if(numb1 == 0 || numb1 == numb2 || numb1 == numb3){
        numb1 = numb1 + 1;
    if(numb2 == 0 || numb2 == numb3 || numb2 == numb3){
        numb2 = numb2 + 1;
    if(numb3 == 0 || numb3 == numb1 || numb3 == numb2){
        numb3 = numb3 + 1;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900