Correct "Rand" expression?

Hi folks, kinda new to C++ and programming in general,
so im figuring out the basics with the help of some exercises.

On topic, is this expression for a random number between
128(including) and 254(including), correct?
(Using srand(time(NULL)) .)

x=128+rand()%126

Would adding "Sleep(rand()%500)" before picking the above
number make the distribution even more... uneven(random)?

Is there a better way to make that pick instead of what i wrote?
(There always is but apparently it's not very obvious for me lol!)

Thanks in advance!
(i would add the whole code intact but i could get "charged"
of copy-paste'ing from the internet.. :-| )



edit: sorry if i posted this in the wrong section!
Last edited on
x % 126 (x being an integer) yields a number in the range 0-125 (inclusive) by mathematical definition.

therefore 128 + [0-125] yields a range of [128, 253]

you should call srand() exactly once in your program, and thereafter rand() is not affected by passage of time.

Thank you for the correction!
So, if the program picks 5 numbers one after another,
x1=..
x2=..
and so on, with the corrected expression and srand() present after
the variable declarations, those 5 numbers will not be following any
kind of a pattern?
Thank you very much!
I know, i know, "search" is supposed to be used... hehe, i'm sorry about that.
Very comprehensible explanation in that post. For now i'll just keep on using
real-world plastic little balls for any "bingo" games :-)

Have a nice day!
There is nothing wrong with using rand() for bingo.

Just seed rand() with a different value each time you run your program. To do that, call srand( time( 0 ) ) at the beginning of your program exactly once. As long as the clock on your PC keeps ticking you'll get different numbers each time your program runs.

Remember to seed rand () with srand() just once (and only once, not more), but you can seed it with a non static value like the clock of the system (srand(time(NULL))), the only problem with this is that the seed for this method changes every second, and if you run various instructions for retrieving random numbers under the same second, the random number will be the same (you'll have to wait for the clock to change to a new second to obtain a new random number, and you don't want to wait seconds), so, other alternatives are:

1.- seed with "ftime()" (Included in "sys/timeb.h") for milliseconds:

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <sys/timeb.h>

int main()
{
  struct timeb tmb;
  ftime(&tmb);
  srand (tmb.millitm)

  return 0;
}


2.- I donĀ“t remember the function but you can seed the rand () for microseconds.

3.- seed with "gettime ()" (included in "time.h") for nanoseconds (sorry, i don't know how to use it).

4.- finally the fastest and more reliable, seed it with CPU cicles "QueryPerformanceCounter ()" (icluded in "windows.h").

1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

int main()
{
  LARGE_INTEGER cicles;

  QueryPerformanceCounter(&cicles);
  srand (cicles.QuadPart);

  return 0;
}


I almost forgot, visit:
http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx
to understand how "rand ()" works and learn to implement it correctly.
Last edited on
Topic archived. No new replies allowed.