text: ### Random function better than rand()?
  
  So I need my program to very rapidly spit out random numbers. Now I don't remember where I read this, but I recall that rand() works with the clock in some way, such that it switches values every second. For my program, I need a function that switches numbers faster than that. I get output such as:
  12
  12
  12
  12
  12
  12
  12
  12
  12
  514
  514
  514
  514
  514...
  (NOTE: that would be the course of about 2 seconds)
  
  I need output like:
  12
  41
  231
  412
  3123
  423
  3213
  2341
  554
  765
  ...
  (and I need all these random numbers in about 2 seconds)
  
  Is there a way to do this??
  Last edited on
   Now I don't remember where I read this, but I recall that rand() works with the clock in some way
  
  You are mistaken. rand() does not use the clock in any way.
  
  You can use rand just fine for what you are trying to do.
  
  LONG AND LENGTHY EXPLANATION OF HOW PRNGS WORK
  
  PRNGs are basically a mathematical operation designed to take an input number and "scramble it" to produce an output number that looks random.
  
  The output number is then used as the next iteration's input... so it produces a sequence of semi-random numbers.
  
  For example... rand() might look something like this (note: this is conceptual, rand probably looks very different):
  
   ``1234567`` ``````int rand_state = 0; // <- state of the RNG (input and output) int rand() { rand_state = rand_state * 46781267 + 9788973; // scramble the number return rand_state; }``````
  
  This will produce random-looking numbers, however the sequence will always be the same because the starting point (rand_state) is always zero. Therefore you must "seed" the PRNG so that it starts at a different point in the sequence. This is what the 'srand' function is for:
  
   ``1234`` ``````void srand(int seed) { rand_state = seed; }``````
  
  Now... this is where the clock comes in. People usually use the current time to seed the RNG because the clock is almost guaranteed to be different each time the program is run. This ensures that you'll start at a different point in the random sequence, which means you'll get a different stream of random numbers each time the program is run.
  
  example:
  
   ``12345678`` ``````int main() { srand( time(0) ); // <- seed with the current time // print 5 different random numbers: for(int i = 0; i < 5; ++i) cout << rand() << endl; }``````
  
  The problem is that newbies don't understand the difference between srand and rand... and think you have to call them together (when you shouldn't). Thus they often do something like this:
  
   ``12345678910111213`` ``````int random() { // never do this srand( time(0) ); return rand(); } int main() { // generate 5 random numbers (poorly) for(int i = 0; i < 5; ++i) cout << random() << endl; }``````
  
  If you do this... you will get the same number repeated, because you keep resetting the rng with srand after each call to rand... ensuring that it will start at the same point in the sequence (and therefore produce the same number) as it did previously.
  
  So as long as you call srand() exactly once at the start of your program... then rand() will work just fine for your purposes. It may not be the best rng around, but it's decent and is beginner friendly.
  try not making your own functions.because it will make your program not to run on other computers.
  Last edited on
   try not making your own functions.because it will make your program not to run on other computers.
  
  Waaaaat?
   try not making your own functions.because it will make your program not to run on other computers.
  
  Jeez, i nearly pressed the report button then.
  if you want,search google.i've read this in "how to make c++ programs" book.
  it says "if there's any functions,why making them?the functions will make your program unportable."
  
  note:the program is written by the Eliwet brothers.
  Last edited on
  jesus dude, stop typing and read this:
  http://www.cplusplus.com/doc/tutorial/functions/
  so...what are you saying?the book is a lie?
  Is there a link to this book online anywhere? What is the context of that statement about being unportable?
  i've got it a from the library.but i will tell you if i get the site.
  the statement is:maybe this function will work on few computers,but some computers does'nt run the program.that's what the book said.
  Last edited on
  Nothing about functions is unportable. You are either misunderstanding the book, or that is the single worst programming book ever written.
  
  It's actually impossible to write a C++ program without functions (at the very minimum, you need 'main', which is a function). Functions are a key part of the language. They make your code reusable, and they make it easier to follow and understand.
  Is there an ISBN number on the book that you can post here?
  function attributes, functions that make calls to posix or winapi functions, functions that use the c++11 stl on a compiler that doesnt have (full) c++11 support
  Disch wrote:
  Little Bobby Tables wrote:
  function attributes, functions that make calls to posix or winapi functions, functions that use the c++11 stl on a compiler that doesnt have (full) c++11 support
  
  None of these things make functions "unportable" since they would presumably be used in code without functions as well. There is nothing unportable about functions.
  
  Little Bobby Tables:
  
  I was going to respond to that, but I'm not going to waste my time. You know what I mean.
  I suspect this book is a "C++ as she is wrote" sort of deal: a translation of a C++ programming book in language A to language B by a non-programmer speaker of language C.
  @Disch: alright
  @cire: if i use a function attribute, that isnt guaranteed to be portable. it is a feature of functions, and yet isnt neccesarily portable. true you can write code without them, but that doesnt change the fact they are guaranteed portable
   @cire: if i use a function attribute, that isnt guaranteed to be portable
  
  If you use an implementation defined attribute, whether it applies to a function or some other construct, the lack of portability is a property of the implementation defined attribute. It is not a property of functions. Functions are portable.
  Topic archived. No new replies allowed.