Javascript随机引用生成器NO REPEAT

时间:2013-12-27 18:16:49

标签: javascript random generator quotes repeat

请参考www.thisyeariwantto.com这是我用来显示“随机”引号的js代码......但我遇到的问题是数组上的引号会重复。我希望用户进行点击和点击,而不会看到相同的报价两次。我怎样才能做到这一点?提前谢谢。

function jargonator(){
$('#head').text("This year I want to...");
$('#tag').text("");

var fragments = shuffle(
["gym","Fall in love(cliche), be happy and work abroad","stop fighting with my boyfriend","to finish things, no matter how long they take or how silly they are", "I'LL FINISH EVERY THING I START !","adopt a Koala","write a book","make a million","grow a pair","get married","drink less live more","figure out where i buried her","Give my girlfriend a real orgasm","Read more books","Save more money","Lose weight","Redecorate","Take better photos so that I can gain more instagram followers","Stop it with the #selfies","Travel","Stop cheating on my husband","Sell old unwanted stuff on eBay","Do something for charity","Get new boobs","Spend more time with kids","Spend Less time on Facebook","Totally revamp my wardrobe","Try a new hairstyle... down there ;)","Have a threesome","Get a six-pack... of premium artisanal beer","Eat less chocolate","Socialise more in real life rather than Facebook","Drink less alcohol","Eat an entire bowl of Ben & Jerrys without feeling guilty", "Start my own business","Recommend this site to all my friends","Tell Susan I have feelings for her","Stop hitting my girlfriend","Do less cocaine","Quit smoking","Get a promotion","stop saying, 'Ooh, that feels nice' whenever the security guys frisk me at airports.","work with neglected children. (my own)","balance my checkbook. (on my nose).","Learn how to use Twitter","Stop sleeping with my brother’s fiancée","Have a better relationship with my parents","do less laundry and use more deodorant","assure my lawyer that I will never again show up drunk at a custody hearing.","start shaving my legs again","Run a half or full marathon","Call people more than text","Stop texting 'LOL'","Stop sexting my cousin", "Watch less reality TV","Stop treating my cat like a real person","Stop buying every iphone that comes out","Stop sleeping with my ex","Stop faking my orgasms","go to the beach more often","get penis reduction surgery so my girl lets me fuck her in the ass","start pretending i'm gay so I can get free drinks at the rainbow room","stop lying on my resume","Exclude McDonald's from my daily diet","Stop considering ketchup a vegetable","stop pretending I have friends","Make at least one REAL friend","Learn how to spell 'thru'... 'thrugh'... 'trhouh'.... fuck it.","Get into a fight so I can finally use my mma skills","stop watching mma so my girlfriend stops thinking I'm gay","Paint my balls blue so I always have an excuse for my gf to jerk me off","Tell Rebecca to fuck off, she is such a bitch","tell my son he is adopted, and his real name is not Kyle... it is Rodrigo.","Stop being a hipster because everyone is doing it","Stop being a hipster, thats so 2013","stop taking naked pictures in snapchat","Reduce, reuse, recycle :)","Graduate!","Be happy!","Take more pictures","Learn how to twerk","stick my tongue out and not feel dirty...like Miley","ride naked on a wrecking ball...with a hammer","make my first legal pornographic movie...with a hidden camera...","learn japanese curse words","star on a rap video as one of the hoes in the back","get my freak on...it's been off way too long...and people are talking","stop showing my boobs so I get more likes","make my momma proud","stop sleeping with my boss's daughter","wear condoms more often","tell him he is not the real dad...","Have intense lesbian sex in public places.","stop making new year's resolutions.","finally dunk.","stop doing Molly","Eat the still beating Heart of Jeff Gordan","get rich doing what i like","Do the splits!","speak up","finally open my own practice","meet a girl","be more positive","Stop being an intern and get a job.","travel to where the soul meets body.","get a tattoo","be fearless","stop curating, start creating", "continue to make my boyfriend happy to the best of my ability.","leave The City and start really living","become a mermaid","Quit 'Call me maybe'","Do my Irish penpal", "Have more bacon, have more sex","study quantum physics so I can RULE THE WORLD","stop biting my nails","drink quality instead of quantity","sleep completely naked in a middle of a peaceful forest","Take a shower with Ryan Gosling","break the circle of no-life", "BOOMSHAKALAKA!!!","Break the Internet", "go back to the future","play hide & seek with strangers on the internet","file a complaint to the Karma Police","NOT fall in love","Stop making a Morgan Freeman voice when talking about Nelson Mandela's death","Make my husband allergic to viagra... i am tired!! :)","cast a worldwide campaign to protest against animal abuse","drink over 20 Mezcal shots in a row","star in the next Star Wars movie","get inspired","stop thinking 'It'll be our year... I'm the only one being the Zombie","Try and see if I can live on wine and sushi... and nothing else","Prove Einstein was wrong","Buy a hammock and work from there","forget about Teen Spirit","find the second star to the right, and go straight on till morning","Leave Narcissus alone","write a novel","make the team","go to all my AA meetings","spit in the face of convention","Somewhere I've never been yet","learn a new recipe", "Paint!", "learn to meditate and at least take one weekend for myself to travel.",]
)

$('#c').click(function () {
    change(1000);    
});

function change(time){
    $('#tag').animate({
        opacity: 0
    }, time, function () {
        $('#tag').html(fragments.pop()).animate({
            opacity: 1
        }, time)
    });
}

function shuffle(o) { //v1.0
    for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};
change(0);

}    

我真的需要帮助。如果您需要查看任何其他文件的任何其他代码,请告诉我。非常感谢你。

2 个答案:

答案 0 :(得分:0)

你可以从数组中弹出它

for( var i=0;i<fragments.length;i++){
   str += fragments[i].pop();
   if(i <fragments.length-1){str+=stub;}
}

如果你这样做,那么它会删除引用而你永远不会有同意。

在执行此操作之前,您可以对数组进行随机排序,然后每次都会有不同的引号

类似于this

答案 1 :(得分:0)

我们无法像您期望的那样控制Math.random()返回的值。但是,尝试这种解决方法,每次生成一个值时,将其添加到另一个数组,比如mylist

var fragments =[
["quote1", "quote2", "quote3","etc", ... ]

var selected;
var temp;

var str = "";
var stub = "";
for( var i=0;i<fragments.length;i++){
temp = fragments[i][Math.floor(Math.random()*fragments[i].length)];
while(selected.contains(temp)){
temp = fragments[i][Math.floor(Math.random()*fragments[i].length)];
}

selected.push(temp);
str += temp;
if(i <fragments.length-1){str+=stub;}
}




$('#tag').animate({opacity:0.9},1000).append(str);

}    

最终在某个时候所有的random都会在明确的mylist之后生成并继续。至少随机不会重复,直到片段中的所有元素都被选中一次。