Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,687 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,510 people online right now. Registration is fast and FREE... Join Now!




better random # theory?

 
Reply to this topicStart new topic

better random # theory?

marla
post 20 Nov, 2005 - 02:45 PM
Post #1


New D.I.C Head

*
Joined: 17 Nov, 2005
Posts: 14



Thanked 1 times
My Contributions


right now for random # generation i am using:

CODE

int myRandom(void)
{
    srand (time(NULL));
    return 1 + (rand()%255);
}


however, when the function is being used to rapidly insert random numbers into a string, the numbers are incremental or the same even. is there a better method for "MORE RANDOM" random #'s?
User is offlineProfile CardPM

Go to the top of the page


Dark_Nexus
post 20 Nov, 2005 - 02:59 PM
Post #2


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 1 times

Dream Kudos: 625
My Contributions


I believe the problem occurs when you seed the randon number using srand(...) everytime; you should only need to seed the number once.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Nov, 2005 - 06:43 PM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


Nexus is correct (as always!)...you mention that you are using tis for rapid generation, so likely the function is being called one after another...the random generator is seeded with virtually the same time value each pass, used once, then reseeded. Seeding one time in your main function, then generating the random number in your function should provide the results you are looking for.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 08:01AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month