0

I am trying to get random unique number and assign it to variable .for example :

var nums=[0,1,2,3,4,5,6,7];

then i have 8 variables and i want to take one of these numbers, also to be unique, every time i refresh the page. I'm not very good at Javascript, so please help.

7
  • 2
    JavaScript can't help here. Only server-side can do what you want. Commented Jan 30, 2013 at 14:00
  • @VisioN There could be a new JavaScript API that enables this. I'm not sure, but I think I've come across something like that a while ago. Commented Jan 30, 2013 at 14:05
  • @ŠimeVidas Well, TBH we can use localStorage and even cookies for that. But I'd go for server-side solution anyway. Commented Jan 30, 2013 at 14:10
  • 1
    You can do it from JS. Generate the random number r = nums[Math.floor(Math.random() * 7)] and append it to a cookie each time. Commented Jan 30, 2013 at 14:10
  • 2
    @techfoobar I don't think that Math.random() qualifies as truly random. Commented Jan 30, 2013 at 14:12

5 Answers 5

2

If the number only needs to be unique on the client side, it's possible. You could create a number based off the current time for example:

var id = new Date().getTime();

However, if this unique ID needs to be unique for every client, as the comments in your OP state, you'll need a server-sided solution:

var id = "<?php echo uniqid() ?>";
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for answering,actually i expirimented and created this code var a = Math.floor(Math.random()*6); var b =Math.floor(Math.random()*6); var c=Math.floor(Math.random()*6); var d=Math.floor(Math.random()*6); var e=Math.floor(Math.random()*6); var f=Math.floor(Math.random()*6); and then i make a while and make comparision if some of the variable mathes to loop until it`s not matching and assign it to the variables again,but this is quite long and wrong code
If you execute this pro grammatically more than once in a row, you could get a collision .
1

First off, to get a random value from the array, you could do this:

var nums = [0,1,2,3,4,5,6,7];
var num = Math.floor(Math.random() * nums.length);

alert(num);

If you needed it to be unique, then it depends on what unique should mean. If you mean that for one user it should never repeat a number until they'd all been exhausted, then you could use a cookie to keep track of which had been shown.

Comments

0
 function randomVarGenerator()
    {
    var s4=function()
    {
       return Math.floor(Math.random(0,9)*10).toString();

    };
    return (S4() + S4() + S4() + S4()  + S4() + S4()+ S4() + S4());
    }
randomVarGenerator();

if its generating uniqueString

 function randomVarGenerator()
    {
    var s4=function()
    {
       return Math.floor(Math.random() * 0x10000 /* 65536 */).toString(16);

    };
    return (S4() + S4() + S4() + S4()  + S4() + S4()+ S4() + S4());
    }

randomVarGenerator();

1 Comment

This does not guarantee uniqueness, and I don't think it is RFC compliant either. Just a simple way to get a guid-like number.
0

I guess this will be help to you. * First you should create an array that includes number. * Then you should create another array * Then randomly pick a number from this array and place it to another * After that remove the picked element from the number array.

function rand(n){

var numArr = new Array("0","1","2","3","4","5","6","7","8","9","10");


var pickArr = new Array(); 

var length=16;

for(var i=0;i<n;i++){
        var num=numArr[Math.floor((Math.random()*length))];
        pickArr[pickArr.length]=num
        length--;
        numArr.splice(findRank(numArr,num), 1);

    }


    return pickArr;
}

function findRank(seq,n){
    var cnt=0;
    var flag=true;
    for(var i=0;i<seq.length&&flag;i++){
        if(seq[i]==n)flag=false;
        else cnt++;
    }

    return cnt;
} 

Comments

0
var n = 4, uniqueRandoms = [];
function makeUniqueRandom() {
    if (!uniqueRandoms.length) {
        for (var i = 1; i < n; i++) {
            uniqueRandoms.push(i); // generates [1,2,3,4]
        }
    }
    var index = Math.floor(Math.random() * uniqueRandoms.length),
        val = uniqueRandoms[index];
    uniqueRandoms.splice(index, 1);
    return val;
}

makeUniqueRandom(4); // use

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.