C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-15-2009, 09:18 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 4
Question rand() gives only one number...

Hey.
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   Reply With Quote
Old 07-15-2009, 09:26 AM   #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   Reply With Quote
Old 07-15-2009, 09:29 AM   #3
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 07-15-2009, 09:42 AM   #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   Reply With Quote
Old 07-15-2009, 09:47 AM   #5
Guest
 
Sebastiani's Avatar
 
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:
Header File

stdlib.h

Category

Math Routines

Syntax

#include <stdlib.h>
void srand(unsigned seed);

Description

Initializes random number generator.

The random number generator is reinitialized by calling srand with an argument value of 1. It can be set to a new starting point by calling srand with a given seed number.

Return Value

None.
Exercise: Now look up the documentation for the 'time' function. Things will be much clearer then.
Sebastiani is offline   Reply With Quote
Old 07-15-2009, 09:58 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-15-2009, 09:58 AM   #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   Reply With Quote
Old 07-15-2009, 09:59 AM   #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;
}
It's still your job to figure out how this works.
__________________
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-15-2009, 10:22 AM   #9
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 07-15-2009, 10:40 AM   #10
Registered User
 
Join Date: Jul 2009
Posts: 4
Ahh i get it now, Thanks a lot!
J0nathan is offline   Reply With Quote
Reply

Tags
cmath, error, rand(), random

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22