Random Function

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Random Function

Post 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 ;).
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Function

Post by MarauderIIC »

dandymcgee wrote:Top Secret level encryption,
I would be willing to bet that this doesn't use rand().
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Random Function

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Random Function

Post 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?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Random Function

Post 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 :(.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Random Function

Post 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 ;).
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Function

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Random Function

Post 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 ;).
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Function

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Random Function

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Random Function

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Random Function

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Function

Post by MarauderIIC »

You said Top Secret.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Random Function

Post 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. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Random Function

Post 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
Post Reply