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.
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!