Click to See Complete Forum and Search --> : generating a unique id using javascript which is in the range of short
rameshk
04-07-2003, 01:32 AM
i want 2 know how to generate a unique id using javascript .
i have 2 follow 2 things while generating a unique id .
1.) it should be in short range (1 to 32,767)
2.) without refering to previous generated id's i have to generate the other id's
Do any one have idea or clue to do this ???
Regards ,
K.Ramesh
khalidali63
04-07-2003, 01:39 AM
Here is the one way of doing it.
create an array to store generated number
generate a random number
>0 and < then 32767
var rand = 1 + Math.floor((Math.random()*32767))
then put this number in an array
now when array has elements
go to while loop and validate generated number it must not exist in the list if does exist keep generating new numbers
Hope this guides
Cheers
Khalid
rameshk
04-07-2003, 01:43 AM
Hi khalidali63,
Thanks for ur reply . i have given 2nd condn as not to refer the prev generated id's . but ur solution uses the array to store and refer it again . it shud not happen . can u guide me to do without storing and refering it back again .
Thanks again for ur reply . waiting for ur reply .
Regards ,
K.Ramesh
khalidali63
04-07-2003, 01:51 AM
That almost sounds not workable.
My question to you.If you do not have any reference that a number ever exisited,how can you validate any other number against it?
On the other hand javascript is very limited lang,its not like Java collections that you can validate the existance of a value like Collection.contains("value")
Therefore my feeling is you may have to write nasty code to do this promptly
Khalid
rameshk
04-07-2003, 02:23 AM
Hi ,
thanks again . i am not sure wheather its possible or not .
but i have tried it in different way .
currentTime.getSeconds() will always return the unique id within a miniute (0-59 - With in short range)
currentTime.getSeconds()+""+currentTime.getMinutes()) will always return a unique id within a hour .(0-5959 With in short range)
currentTime.getSeconds()+""+currentTime.getMinutes()+""++currentTime.getHours()) will always return a unique id within a day .(0-595923 NOT With in short range)
so here only my problem arises . if 3rd condn satisfies i will b done . in this way i dont need to store the id's and refer it back . can u help me something similar to this .
waiting for ur reply
Thanks & Regards ,
Ramesh