Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Random Numbers

Reply
Thread Tools

Random Numbers

 
 
Wilson
Guest
Posts: n/a
 
      02-08-2007
I am new to c++ and part of my program needs to create three random
numbers and then output them. I decided to use the computer clock to
help ouput the numbers but i constantly get the same results. I have
used the header file cstdlib for functions rand and srand. Can you
please advise how to alter the program to create random numbers every
time the program is run (or suggest alternate ways)

thanks

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-08-2007
Wilson wrote:
> I am new to c++ and part of my program needs to create three random
> numbers and then output them. I decided to use the computer clock to
> help ouput the numbers but i constantly get the same results. I have
> used the header file cstdlib for functions rand and srand. Can you
> please advise how to alter the program to create random numbers every
> time the program is run (or suggest alternate ways)
>

What program?

Post the relevant code so we can see what you have and how to improve
it. rand() will give the same sequence for the same seed, so you must be
seeding it incorrectly.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Tim Slattery
Guest
Posts: n/a
 
      02-08-2007
"Wilson" <(E-Mail Removed)> wrote:

>I am new to c++ and part of my program needs to create three random
>numbers and then output them. I decided to use the computer clock to
>help ouput the numbers but i constantly get the same results. I have
>used the header file cstdlib for functions rand and srand. Can you
>please advise how to alter the program to create random numbers every
>time the program is run (or suggest alternate ways)


srand((unsigned int)time(NULL));

printf("%d\n", rand());
printf("%d\n", rand());

--
Tim Slattery
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      02-08-2007
"Wilson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>I am new to c++ and part of my program needs to create three random
> numbers and then output them. I decided to use the computer clock to
> help ouput the numbers but i constantly get the same results. I have
> used the header file cstdlib for functions rand and srand. Can you
> please advise how to alter the program to create random numbers every
> time the program is run (or suggest alternate ways)
>
> thanks


This may work for you. Not sure if time() is os dependant.

srand( time() );

std::cout << rand() << "\n";

I think libraries are
#include <time.h>
#include <stdlib>

You might be able to use <ctime>
not sure.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-08-2007
Jim Langston wrote:
> "Wilson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> I am new to c++ and part of my program needs to create three random
>> numbers and then output them. I decided to use the computer clock to
>> help ouput the numbers but i constantly get the same results. I have
>> used the header file cstdlib for functions rand and srand. Can you
>> please advise how to alter the program to create random numbers every
>> time the program is run (or suggest alternate ways)
>>
>> thanks

>
> This may work for you. Not sure if time() is os dependant.
>
> srand( time() );


'time()' is not OS-dependant, but the essence of its return value type
is implementation-specific. Since 'srand' expects 'unsigned', a cast
is an order:

srand ( (unsigned) time() );

Whether it achieves the expected result is implementation-defined, as
well. For all we know, 'time_t' can be such that casting it to
an 'unsigned int' value will always result in, say, 0, which would
defeat the purpose of using the return value of 'time()' as the seed.
IOW, YMMV, but in most cases it works.

>
> std::cout << rand() << "\n";
>
> I think libraries are
> #include <time.h>
> #include <stdlib>


Those are "headers", not "libraries". And it's not <stdlib>, it's
<stdlib.h>.

> You might be able to use <ctime>
> not sure.


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      02-09-2007
On Feb 8, 11:05 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Jim Langston wrote:
> > "Wilson" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed) roups.com...
> >> I am new to c++ and part of my program needs to create three random
> >> numbers and then output them. I decided to use the computer clock to
> >> help ouput the numbers but i constantly get the same results. I have
> >> used the header file cstdlib for functions rand and srand. Can you
> >> please advise how to alter the program to create random numbers every
> >> time the program is run (or suggest alternate ways)

>
> >> thanks

>
> > This may work for you. Not sure if time() is os dependant.

>
> > srand( time() );

>
> 'time()' is not OS-dependant, but the essence of its return value type
> is implementation-specific. Since 'srand' expects 'unsigned', a cast
> is an order:
>
> srand ( (unsigned) time() );
>
> Whether it achieves the expected result is implementation-defined, as
> well. For all we know, 'time_t' can be such that casting it to
> an 'unsigned int' value will always result in, say, 0, which would
> defeat the purpose of using the return value of 'time()' as the seed.
> IOW, YMMV, but in most cases it works.
>
>
>
> > std::cout << rand() << "\n";

>
> > I think libraries are
> > #include <time.h>
> > #include <stdlib>

>
> Those are "headers", not "libraries". And it's not <stdlib>, it's
> <stdlib.h>.


<ctime> and <cstdlib> if you want to be correct.

--
Erik Wikström

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-09-2007
Erik Wikström wrote:
> On Feb 8, 11:05 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> Jim Langston wrote:
>>> I think libraries are
>>> #include <time.h>
>>> #include <stdlib>

>>
>> Those are "headers", not "libraries". And it's not <stdlib>, it's
>> <stdlib.h>.

>
> <ctime> and <cstdlib> if you want to be correct.


Just so that newbies don't get the wrong impression... It's not
"incorrect" to use the .h form of C headers.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      02-09-2007
Erik Wikström wrote:
>>> I think libraries are
>>> #include <time.h>
>>> #include <stdlib>

>> Those are "headers", not "libraries". And it's not <stdlib>, it's
>> <stdlib.h>.

>
> <ctime> and <cstdlib> if you want to be correct.
>


All of <ctime>, <time.h>, <cstdlib>, and <stdlib.h> are correct.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.10 Why aren't my random numbers random? PerlFAQ Server Perl Misc 0 04-27-2011 04:00 AM
FAQ 4.10 Why aren't my random numbers random? PerlFAQ Server Perl Misc 0 02-12-2011 11:00 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How do I get a random number between two random numbers? Alex Untitled Ruby 11 11-16-2009 09:45 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM



Advertisments