LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2011, 04:54 PM   #1
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Rep: Reputation: 1
srand(static_cast<unsigned int>(clock( ))); seems not random enough


Dear c/g++ programmer (on ubuntu linux 11.04):
I copied and test a simple random number generate program
-----------------
#include <algorithm>

#include <vector>

#include <iterator>

#include <iostream>

#include <cstdlib>



using namespace std;



struct RndIntGen

{

RndIntGen(int l, int h)

: low(l), high(h)

{ }

int operator( )( ) const {

return low + (rand( ) % ((high - low) + 1));

}

private:

int low;

int high;

};



int main( ) {

srand(static_cast<unsigned int>(clock( )));

vector<int> v(5);

generate(v.begin( ), v.end( ), RndIntGen(1, 6));

copy(v.begin( ), v.end( ), ostream_iterator<int>(cout, "\n"));

}
--------------------------------------------------------------------
it can compile and run, and for most of people will think it run well, but for me
why it always produce same result, if it is really random and based on clock?
eric@eric-Inspiron-8600:~/cppcookbook/download$ ./a.out
2
5
4
2
6
----------------------------------------------------------
this is from Example 11-13 of book(C++ cookbook), you always can download from
http://examples.oreilly.com/9780596007614/
to test by yourself

Highly appreciate your explanation and thanks a lot in advance, Eric
 
Old 07-21-2011, 05:13 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
it's probably because it should really be time(), not clock(). clock() returns the CPU time used, which depending on the clock frequency, will always be the same right at the beginning when in gets called.
 
Old 07-21-2011, 07:31 PM   #3
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Original Poster
Rep: Reputation: 1
useful theoretically, but not workable yet

I follow your suggestion to modify it to time()
then I get compile error(on my g++4.5.2)
------------
eric@eric-laptop:~/cppcookbook/ch11$ g++ Example11-11.cpp
Example11-11.cpp: In function ‘int main()’:
Example11-11.cpp:14:41: error: too few arguments to function ‘time_t time(time_t*)’
/usr/include/time.h:186:15: note: declared here
----------------------
so can you be specific write out workable code?
srand(static_cast<unsigned int>(time())); // not this, but based on this
thanks a lot in advance, Eric
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what's this? (unsigned int) & 'something' lesca Programming 3 10-31-2010 12:17 AM
unsigned long int uint32_t to unsigned char and back MicahCarrick Programming 2 08-02-2009 01:39 AM
what is the range of unsigned int shamsheer Linux - Newbie 6 04-07-2009 03:00 PM
invalid conversion from 'int' to 'unsigned int*' tigerhp Programming 2 03-02-2008 04:21 PM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration