Skip to content

Instantly share code, notes, and snippets.

@marsen
Last active October 20, 2016 02:20
Show Gist options
  • Save marsen/9883538 to your computer and use it in GitHub Desktop.
Save marsen/9883538 to your computer and use it in GitHub Desktop.
//隨機GUID
function RandomGUID(){
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
//隨機整數
function RandomNum(Min,Max){
if(Max > Min){
//return Math.floor(Math.random() * (Max - Min + 1)) + Min;
return ~~((Max-Min) * Math.random())+Min;
}else{
console.log("function RandomNum exception");
}
}
//隨機日期 random date
function getRandomDate(from, to) {
if (!from) {
from = new Date(1900, 0, 1).getTime();
} else {
from = from.getTime();
}
if (!to) {
to = new Date(2100, 0, 1).getTime();
} else {
to = to.getTime();
}
return new Date(from + Math.random() * (to - from));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment