![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 4
| Im a new C++ programmer and having trouble using rand() in programs. the only number rand returns is 41. Is there anything I can do to make it generate other numbers? Or is there any better generators? Thank you for reading this thread and have a good day =). |
| J0nathan is offline | |
| | #2 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| How are you using it? Are you calling srand() first? Post an example.
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> Is there anything I can do to make it generate other numbers? Of course not. Why would you want any other number? How useful would that be? >> Or is there any better generators? The implementation of rand is not dictated by the standard, so really just depends on how it is implemented on your platform. >> Im a new C++ programmer and having trouble using rand() in programs. You need to seed your generator before you first use it. Look up srand. |
| Sebastiani is offline | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 4
| Okay i have looked srand up. But i still don't understand its syntax :S srand(time(NULL)) |
| J0nathan is offline | |
| | #5 | |
| Guest Join Date: Aug 2001
Posts: 4,923
| That has nothing to do with it's syntax, but one particular usage. You should probably get a hold of some decent CRT API documentation. Here's what mine says: Quote:
| |
| Sebastiani is offline | |
| | #6 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Remember that the seed must be initialized with a unique number, because a randomizer is basically just an algorithm applied to a unique number. This is why we see this particular line of code very often. Once you read the documentation on time, it should be clear.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 4
| okay. can you give me a example of rand() where it gives a totally random number and im not sure what time has to do with random numbers. |
| J0nathan is offline | |
| | #8 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Code: int main()
{
std::srand( (unsigned int)std::time(NULL) );
for (int i = 0; i < 10; i++)
std::cout << std::rand() << std::endl;
}
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #9 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> im not sure what time has to do with random numbers. Random number generators are generally not really random, but fully deterministic and thus "psuedo-random". That simply means that if you give a particular implementation the value 3114 it will generate the same sequence of numbers everytime. Naturally, you'll probably want to get different values in most cases, so you need a function that can produce an ever-changing flow of numbers, and the 'time' function is one such source. But there are other possibilities, of course. You could access the CPU temperature, for instance, and produce a seed from that. |
| Sebastiani is offline | |
| | #10 |
| Registered User Join Date: Jul 2009
Posts: 4
| Ahh i get it now, Thanks a lot! |
| J0nathan is offline | |
![]() |
| Tags |
| cmath, error, rand(), random |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help getting program print out the digit in words | cosmiccomputing | C Programming | 26 | 04-24-2008 08:28 AM |
| Need help with this compiler error | Evangeline | C Programming | 7 | 04-05-2008 09:27 AM |
| Prime number program problem | Guti14 | C Programming | 11 | 08-06-2004 04:25 AM |
| parsing a number | juancardenas | C Programming | 1 | 02-19-2003 01:10 PM |
| Random Number problem in number guessing game... | -leech- | Windows Programming | 8 | 01-15-2002 05:00 PM |