Daniel W. Posted May 20, 2006 Share Posted May 20, 2006 Hi, i am learning C++ at the moment and i just started 2 days ago. And i got i question about the rand() function I got this c++ code: #include <iostream> #include <cstdlib> using namespace std; int main() { unsigned int keim; int z1,z2,z3; cout << "Put in your number: "; cin >> keim; srand(keim); z1 = rand(); z2 = rand(); z3 = rand(); cout << "Your numbers: " << z1 << "\t" << z2 << "\t" << z3 << endl; return 0; } and i always get the same numbers out and i don't know why i translated the code in autoit $box = InputBox("", "Your number?") $1 = Round(Random( 0, $box)) $2 = Round(Random( 0, $box)) $3 = Round(Random( 0, $box)) MsgBox(0, "", $1 & @CRLF & $2 & @CRLF & $3) but why do i get the same numbers in c++ and always different in autoit? --------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote] Link to comment Share on other sites More sharing options...
Developers Jos Posted May 20, 2006 Developers Share Posted May 20, 2006 You need to use SRAND. see here for an explanation SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Uten Posted May 20, 2006 Share Posted May 20, 2006 The way you use srand makes rand return the same numbers all tyhe time. Random generators are not "random". The randomnes is created from a function using initialization values and then time to generate a semingly random number. People have written entier books about the topic so don't despaire Take a look at the page @JdeB provided. You could also take a look at this page. And as you are in the very begining of your C/C++ studies I would recomend to take a look at the "Thinking in C++" books by Bruce Eckel. If I recall right one of the projects in the book is a random generator. Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling Link to comment Share on other sites More sharing options...
Nutster Posted May 21, 2006 Share Posted May 21, 2006 (edited) The random number generator that comes with C/C++ is pretty simple; it is just a two-statement function. The one I put in AutoIt is a more advanced generator. Take a look at the Random() function documentation for more details. Also, when AutoIt starts, it calls the srand equivilant for the generator that we are using, so you should not get the same sequence of numbers each time, as you noted. Edit: Fox spelling error. Edited August 17, 2006 by Nutster David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd... Link to comment Share on other sites More sharing options...
jftuga Posted May 21, 2006 Share Posted May 21, 2006 Here is some C code, using seeds srand() via gettimeofday(). This way, you will always get a different set of random numbers, from 0 to 254. It compiles under Linux and am not sure about Windows. I hope this helps anyway. -John #include <stdio.h> #include <stdlib.h> #include <sys/time.h> int main (int argc, char *argv[]) { struct timeval t; struct timezone z; int i; gettimeofday(&t, &z); srand( t.tv_usec ); for(i=0; i < 5; i++) { printf("%d ", rand() % 255); } printf("\n"); return 0; } Admin_Popup, show computer info or launch shellRemote Manager, facilitates connecting to RDP / VNCProc_Watch, reprioritize cpu intensive processesUDF: _ini_to_dict, transforms ini file entries into variablesUDF: monitor_resolutions, returns resolutions of multiple monitorsReport Computer Problem, for your IT help deskProfile Fixer, fixes a 'missing' AD user profile Link to comment Share on other sites More sharing options...
cppman Posted May 23, 2006 Share Posted May 23, 2006 (edited) I had the same question when i started to learn c++. Play with this code a little: #include <ctime> #include <iostream> using namespace std; int main() { srand(time(NULL)); //seeds the random number generator. int r = (rand() % 10) + 1; //Generates a random number 1 through 10. cout << r; return 0; } i think this code is right... lol btw, a good way to get started is getting an "in easy steps" book called, "c++ programming", its really good. I started with that book then got a better book called SAMS teach yourself c++ in 24 hours. The SAMS book is: 502 pages The "in easy steps" book is: 192 pages. Edited May 23, 2006 by CHRIS95219 Miva OS Project Link to comment Share on other sites More sharing options...
Daniel W. Posted May 23, 2006 Author Share Posted May 23, 2006 Well thank you all I am german and i already got a book. Its called "C++ lernen und professionell anwenden" in german, in english it would be "C++ learning and using it professionell" --------------------------------------------------------------------------------------------------------------------------------Scripts : _Encrypt UDF_UniquePCCode UDF MS like calculatorInstall programm *UPDATED* --------------------------------------------------------------------------------------------------------------------------------[quote name='Helge' post='213117' date='Jul 26 2006, 10:22 AM']Have you ever tried surfing the internet with a milk-carton ?This is similar to what you're trying to do.[/quote] Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now