Page 2 of 2

Re: Random Function

Posted: Thu Jun 25, 2009 9:05 pm
by zodiac976
avansc wrote:thats what you call spin. you should be in politics.
People like to see stuff done in different ways
if everyone programmed the same that would
be very bad and I hate politics. I will stick with
programming ;).

Re: Random Function

Posted: Thu Jun 25, 2009 9:06 pm
by MarauderIIC
dandymcgee wrote:Top Secret level encryption,
I would be willing to bet that this doesn't use rand().

Re: Random Function

Posted: Thu Jun 25, 2009 9:08 pm
by zodiac976
MarauderIIC wrote:
dandymcgee wrote:Top Secret level encryption,
I would be willing to bet that this doesn't use rand().
Something more sophisticated in my opinion.

Re: Random Function

Posted: Thu Jun 25, 2009 9:13 pm
by avansc
zodiac976 wrote:
MarauderIIC wrote:
dandymcgee wrote:Top Secret level encryption,
I would be willing to bet that this doesn't use rand().
Something more sophisticated in my opinion.
like?

Re: Random Function

Posted: Thu Jun 25, 2009 9:14 pm
by zodiac976
In college I had to learn about randomizing on my own
and put it in my mid-term and scored good on it.

I wanted to post my own version of how I did it even
though it isn't the absolute perfect solution :(.

Re: Random Function

Posted: Thu Jun 25, 2009 9:14 pm
by zodiac976
avansc wrote:
zodiac976 wrote:
MarauderIIC wrote:
dandymcgee wrote:Top Secret level encryption,
I would be willing to bet that this doesn't use rand().
Something more sophisticated in my opinion.
like?
Don't ask me I am not that smart ;).

Re: Random Function

Posted: Thu Jun 25, 2009 9:18 pm
by MarauderIIC
avansc wrote:like?
Like some calculation of Pi.

Oh, and to sum it up for the OP:
RandomDever wrote:Is there a way to have a random range function that never repeats any digit? :|
No. If you don't reseed by calling srand(), they seem to repeat after a few calls to rand(). If you do reseed, and you use time(NULL) to seed with every time, you'll notice more repetition than seeding with the previously generated random number, but it will eventually repeat, either way.

Look at it this way, unsigned ints have a range of 0-4,294,967,295
If you call rand() 4,294,967,297 times, you will have at least one number repeat. But since I doubt that you want 4,294,967,296 unique random numbers -- rather, you probably only want like, 10 -- then after 10 calls, you are guaranteed to repeat. (This is the common-sense Pigeonhole principle, which you can do all sorts of fun things with. For example, if the maximum hairs on a head is 1 million and there are 1 .5 million people in London, then at least 2 people in London have the exact same number of hairs on their head).

In a perfect world, as you approach the 5th number, you'll have a 50% chance of your 6th number being a repeat. As you approach the 9th number, you have a 90% chance of your 10th number being a repeat. But since computers are psuedo-random, as opposed to truly random, your chances of getting a duplicate result always seem to be higher. You can reduce the chance of getting a duplicate by seeding with time(NULL) on the first call and reseeding with the last result of rand() [pure, not modded down to your range] on each subsequent call to rand().

Hopefully that totally answers the question.

Re: Random Function

Posted: Thu Jun 25, 2009 9:23 pm
by zodiac976
MarauderIIC wrote:
avansc wrote:like?
Like some calculation of Pi.

Oh, and to sum it up for the OP:
RandomDever wrote:Is there a way to have a random range function that never repeats any digit? :|
No. If you don't reseed by calling srand(), they seem to repeat after a few calls to rand(). If you do reseed, and you use time(NULL) to seed with every time, you'll notice more repetition than seeding with the previously generated random number, but it will eventually repeat, either way.

Look at it this way, unsigned ints have a range of 0-4,294,967,295
If you call rand() 4,294,967,297 times, you will have at least one number repeat. But since I doubt that you want 4,294,967,296 unique random numbers -- rather, you probably only want like, 10 -- then after 10 calls, you are guaranteed to repeat. (This is the common-sense Pigeonhole principle, which you can do all sorts of fun things with. For example, if the maximum hairs on a head is 1 million and there are 1 .5 million people in London, then at least 2 people in London have the exact same number of hairs on their head).

In a perfect world, as you approach the 5th number, you'll have a 50% chance of your 6th number being a repeat. As you approach the 9th number, you have a 90% chance of your 10th number being a repeat. But since computers are psuedo-random, as opposed to truly random, your chances of getting a duplicate result always seem to be higher. You can reduce the chance of getting a duplicate by seeding with time(NULL) on the first call and reseeding with the last result of rand() [pure, not modded down to your range] on each subsequent call to rand().

Hopefully that totally answers the question.
Well said and I didn't know about reseeding again ;).

Re: Random Function

Posted: Thu Jun 25, 2009 9:25 pm
by MarauderIIC
zodiac976 wrote:Well said and I didn't know about reseeding again ;).
Thanks and http://elysianshadows.com/phpBB3/viewto ... 303#p42303
Which is why avansc asked if you read, I think, since your answer was nearly spot-on to what I had already written.

Re: Random Function

Posted: Thu Jun 25, 2009 9:39 pm
by zodiac976
MarauderIIC wrote:
zodiac976 wrote:Well said and I didn't know about reseeding again ;).
Thanks and http://elysianshadows.com/phpBB3/viewto ... 303#p42303
Which is why avansc asked if you read, I think, since your answer was nearly spot-on to what I had already written.
Sorry, I will avoid it next time.

Re: Random Function

Posted: Thu Jun 25, 2009 10:03 pm
by avansc
funny enough the biggest thing in cryptography is not random numbers, but primes.
come to think of it. random plays little to no role in cryptography.

like DES, MD-n, SHA.. all those dont have any randomization in them.

Re: Random Function

Posted: Fri Jun 26, 2009 12:10 am
by dandymcgee
MarauderIIC wrote:
dandymcgee wrote:Top Secret level encryption,
I would be willing to bet that this doesn't use rand().
Depending on the organization.. I'll take that bet. :lol:

Re: Random Function

Posted: Fri Jun 26, 2009 12:47 am
by MarauderIIC
You said Top Secret.

Re: Random Function

Posted: Tue Jul 07, 2009 6:12 pm
by Ginto8
another (you could say more random) way of seeding it is something like this:

Code: Select all

srand((unsigned)time(NULL));
srand(rand()%3000);
srand(rand()%3000);
if you do that before each random number, it would be a (little bit of a) waste of CPU power, but it would probably make it seem a little more random. ;)

Re: Random Function

Posted: Wed Jul 08, 2009 2:48 am
by Scoody
Ginto8 wrote:another (you could say more random) way of seeding it is something like this:

Code: Select all

srand((unsigned)time(NULL));
srand(rand()%3000);
srand(rand()%3000);
if you do that before each random number, it would be a (little bit of a) waste of CPU power, but it would probably make it seem a little more random. ;)
It's just a waste of CPU time.
rand() creates the same "random" numbers if seeded the same number; so seeding rand() with a rand() would yield the same numbers anyway.
My point being, it won't be any more random :p