Support » Plugins and Hacks » Hacks » Please help me turn this random number generator into a function.

  • I have a random number generator:

    private static uint GetUint()
        m_z = 36969 * (m_z & 65535) + (m_z >> 16);
        m_w = 18000 * (m_w & 65535) + (m_w >> 16);
        return (m_z << 16) + m_w;
    public static double GetUniform()
        // 0 <= u < 2^32
        uint u = GetUint();
        // The magic number below is 1/(2^32 + 2).
        // The result is strictly between 0 and 1.
        return (u + 1.0) * 2.328306435454494e-10;

    It is said that I can set the two seeds with SetSeed(). The page that I found this on is:

    I am trying to turn this into a working function in which a new number shows up above every comment. The first seed is the last two digits of the comment_ID, and the second seed is the second of the_time, when the comment was posted.

    The reason I am doing this is so that every comment has a new random number, but that number stays the same to the visitors from the moment the comment is posted, no matter who views the comment or whether they’ve refreshed the page or not.

    I am not educated in PHP, but I will take any advice that is given and try to piece it together until my function works. So any help at all will be appreciated. Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz


    For your intended use, you do not want a random number, you want a unique number. Granted, a large random number is highly likely to be unique, but a hash is much much more likely to be unique. Why not do an MD5 hash of the ID and timestamp concatenated together?

    If a MD5 hash is too cumbersome, generating a nonce would yield a less cumbersome hexdecimal number. For that matter, the comment ID assigned by mySQL is unique, though if you were to delete comments, I’m unsure if those numbers get recycled or not

    Furthermore, there are several random number and hash functions in PHP that are more than adequate for virtually any application, no need to reinvent the wheel. For random numbers, if mt_rand() does not meet your needs, openssl_random_pseudo_bytes() certainly will.

    Absolutely not. I know what you’re trying to suggest, and it won’t help me one bit.

    I am NOT looking for just “a random number”. I am trying to get a pseudorandom number above every comment, a new random pick for each comment; each number individually needs to be the same for everyone, all the time, and it needs to be a number 1-x.

    It actually isn’t the number I’m looking for, but the more specific I am, the less people will help me– that much has been proven over time. I might sound a little rude here, but every time I don’t explain enough, people ask the same questions over and over, trying to give me advice for a solution other than what I am asking for; but if I explain too much, nobody answers, because either a) people don’t want to read a lot, or b) people get confused and don’t think they know the answer to my question, when they really do know how to do it if they break it down into steps. So I have been resorting to asking one simple thing at a time until I can put all the pieces together myself and have a working function. I’ve been asking for support on this function for years now in various support forums and chats. It’s extremely frustrating when I have to keep telling people to focus on my question instead of suggesting a different solution. I’m sorry if that sounds rude, but this really is very frustrating.

    If you must know, I’m actually trying to create a dice roll game. The game “rolls the dice” above every comment, and the “roll” is displayed by one of several pictures in a directory, each of one of six dice sides. Every comment has a new random roll, and every visitor will always see that comment with that roll, no matter the server, how many times they refresh, or who the visitor is.

    I already know how this can be done– I know the steps to get there, so I am simply asking for the solution to a single step.

    Are you looking to get a random number between 1 and 6 (inclusive)? If that’s all that it is, then it’s a while lot more easy then you’re trying to make it there. Why not something like..

    function get_rand() {
        return mt_rand (1, 6);

    Then you can store that with your posts data and retireve it when needed.

    You mean I could store it in the database somewhere? That would be great, if I knew how to do that.

    Maybe this is what I should be asking for help with from now on.

    Yeah you can. Again, that’s a simple process.

    update_post_meta ($post->ID, "random_number", $random_number)

    There is more to it then just that, but that’s a starting point.

    There’s a whole lot of info in the codes, but for that you’d mostly want the update_post_meta() page.

    Moderator bcworkz


    catacaustic has given you, as usual, excellent advice, except for the fact you are dealing with comments, not posts, so the roll state should be stored in commentmeta, not postmeta, so the key function will be update_comment_meta().

    Your frustration is clear, and understandable. I can see that some would not want to help you when you divulge you are implementing a dice game, they are idiots IMO. There are thousands of chance games on the ‘net, BFD. I hope you can see now that asking a straight forward question in the right place will yield the best results 😉

    Asking obtuse questions yields obtuse answers and wastes everyone’s time. Good luck with your project, I’m still not sure what you’re really up to, nor do I care, but it sounds like it could be fun!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Please help me turn this random number generator into a function.’ is closed to new replies.