Don't try and insult me by saying I can't read. Granted I didn't type it correctly in my previous post, but in the code its right. I copied it straight from what he put.Originally Posted by Salem
Don't try and insult me by saying I can't read. Granted I didn't type it correctly in my previous post, but in the code its right. I copied it straight from what he put.Originally Posted by Salem
Prove it.That is all good and dandy. But I just ran your example and I got the same set of numbers each time a ran it, which was like 10 times.
Perhaps your time() is broken and it's always returning -1. That would generate the effect you see.RETURN VALUE
On success, the value of time in seconds since the Epoch is returned. On error, ((time_t)-1) is
returned, and errno is set appropriately.
Investigate this.
How would you like me to do that? Copy and paste? But then you could just say I copied it twice.Originally Posted by 7stud
Salem:
I will investigate it to see if thats the case.
I don't know whats going on, I'm sure the example works as it's suppose to. time() is working fine. I even tried it on another machine. It seems to me that srand() is not working seeing as I get the same numbers. Maybe it's not seeding the generator. And I checked if srand() had a return value and it doesn't so I can't check it.
Last edited by pjharris; 01-06-2006 at 08:57 AM. Reason: addition
So something really basic like
Produces constant output?Code:#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); //seed random number generator for(int j = 0; j<10; j++) { cout << rand() << endl; } cin.get(); return 0; }
Puzzled...
i too was not getting random results when i tested the provided example.. not salem's but the earlier one.. so i was just about to post a little example when salem posted that one.. damn! 1 minute late..
anyhow.. i still have a little issue with the original code.. here is what i found when i tested it.... ?
is the array ever initialized.. ? not that i see...
mine was not working and i did this and now it does..
i have never messed with random_shuffle.. very good to know about.!
hth
<edit> oh and btw.. the small example i used resembling salems last post.. works just fine.... </edit>Code:#include <iostream> #include <algorithm> //random_shuffle() #include <ctime> //time() #include <cstdlib> //srand() using namespace std; int main() { srand(time(0)); //seed random number generator int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //// HERE vvvv for(int i=0; i<10; ++i){ arr[i]=rand(); } for(int j = 0; j<10; j++) { random_shuffle(arr, arr+9); for(int i=0; i<9; i++) { cout<<arr[i]<<endl;; } cout<<endl; } cin.get(); return 0; }
Salem your example works fine for me. It must be random_shuffle() then.
nevermind.. i must be a moron.. let me check..
yes, yes i am
<edit>
hmm yes still am..
but i cant seem to make random_shuffle work either..
good thing i dont need it..
oh and yes i am leaving..
</edit>
Last edited by xhi; 01-06-2006 at 09:37 AM.
is the array ever initialized.. ? not that i see...If you aren't familiar with the STL, then the code may seem strange to you, but you have to provide random_shuffle() with the memory range where the values you want shuffled are located.Code:int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Yes, copy and paste the code your are running. Discussing errors in imaginary code is never very productive.How would you like me to do that? Copy and paste? But then you could just say I copied it twice.
Last edited by 7stud; 01-06-2006 at 01:19 PM.
yes i know.. i over looked that somehow..
i do have a question though.. the results i was getting are what threw me off..
so i run the provided example and get this
no matter how many times i run it.. i get the same thing.. i see now what shuffle is doing.. and that the array was initialized.. duh! but i dont understand why the shuffling is the same everytime..Code:283467159 192345876 916472583 216895734 193624857 945128376 728134596 194625783 279613845 328956741
i would think that it would be different in different program executions also..
get what im saying??
Thats the code I'm runningCode:#include <iostream> #include <algorithm> //random_shuffle() #include <ctime> //time() #include <cstdlib> //srand() using namespace std; int main() { srand(time(0)); int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; for(int j = 0; j<10; j++) { random_shuffle(arr, arr+9); for(int i=0; i<9; i++) { cout<<arr[i]; } cout<<endl; } cin.get(); return 0; }
and thats the outputCode:[pjharris@rundi]$g++ board.cc -o board [pjharris@rundi]$./board;sleep 2;./board 269543178 587942631 893761425 849256317 659381274 583127694 638412957 265381497 947168235 749263158 269543178 587942631 893761425 849256317 659381274 583127694 638412957 265381497 947168235 749263158 [pjharris@rundi]$
You should get the same thing if you run it twice in a second. Try running it a few seconds apart.
[edit]
Hey, you beat me to it.
Since you're running the program a few seconds apart, random_shuffle() must use its own random number generator (and not rand()).
Try this code:
[/edit]Code:#include <iostream> #include <ctime> #include <cstdlib> int main(void) { srand(time(0)); for(int x = 0; x < 10; x ++) { std::cout << rand() % 10; } std::cout << std::endl; return 0; }
Last edited by dwks; 01-06-2006 at 01:46 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thats what the sleep does. Sleep 2 says wait two seconds then run it again And it's not the case about timing, because I ran it 10, 30, 60 mins after and I still got the same. I've been getting the same output for 2 days now.Originally Posted by dwks
I know, see my edit.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.