1

I am using

#include<conio.h>
#include<stdlib.h>

to generate a random number from 1-10.

the code is:

rando = rand() % 10 + 1;

for some reason 2 keep being generated. Is there something wrong with my phrasing?

BucketsOstuff
  • 19
  • 1
  • 4
  • 1
    How did you call `srand()`? – timrau Oct 08 '14 at 07:27
  • 1
    Why is 2 not random for you? – Daniel Kamil Kozar Oct 08 '14 at 07:30
  • Have you called `srand()` anywhere in your program? – timrau Oct 08 '14 at 07:30
  • Well it's consistently 2, nothing else. Also when I change the `rand() % 10 + 1;` to `rand() % 10 +2;` It consistently generates 3. – BucketsOstuff Oct 08 '14 at 07:31
  • No I have only called `rand()` – BucketsOstuff Oct 08 '14 at 07:32
  • 2
    Can't resist: http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/00000/2000/300/2318/2318.strip.gif – Thirler Oct 08 '14 at 07:34
  • BTW, with C++11, you are encouraged to use `` facilities instead. – Jarod42 Oct 08 '14 at 07:35
  • Show compilable code that reproduces the problem. Otherwise we are just guessing. – Martin York Oct 08 '14 at 07:59
  • You shouldn't use `rand()` in C++11 and newer. You may want to [read this answer](http://stackoverflow.com/a/22022099/1629821) for more details. – Mr.C64 Oct 08 '14 at 08:41
  • @Jarod42 By whom? The two facilities are different, and address different uses. For just playing around, games and homework, `rand()` is generally sufficient; the extra complexity of `` is really only justified for serious analytics. – James Kanze Oct 08 '14 at 09:01
  • 1
    @Thirler, maybe nine is [guaranteed to be random](http://xkcd.com/221/) as well. – utnapistim Oct 08 '14 at 09:09
  • @JamesKanze: `std::uniform_int_distribution` seems more simple and intuitive than (biaised) `rand() % 10 + 1`. – Jarod42 Oct 08 '14 at 09:27
  • possible duplicate of [Why do I always get the same sequence of random numbers with rand()?](http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand) – XCS Oct 08 '14 at 10:12
  • @Jarod42 Have a look at the examples in some of the answers, and tell me that they aren't overly complicated. (If you need a non-uniform distribution, or if you need some sort of guaranteed quality of the random numbers, the new versions win out, because getting either with the old `rand()` requires a _lot_ of extra code. But for a lot of simple projects, having to create instances of three different types is just excess complexity.) – James Kanze Oct 08 '14 at 10:52

4 Answers4

4

To generate random nuber from 1 to 10, you should really use rand() % 10 + 1 but firstly needed initialization of random seed, look at example:

  #include <stdio.h>      /* printf, NULL */
  #include <stdlib.h>     /* srand, rand */
  #include <time.h> 

  int iSecret ;

  /* initialize random seed: */
  srand (time(NULL));

  /* generate secret number between 1 and 10: */
  iSecret = rand() % 10 + 1;
Ajay
  • 18,086
  • 12
  • 59
  • 105
Maksym
  • 41
  • 7
  • 1
    **Note:** unless you know *why* you're doing otherwise, `srand()` should be invoked **once** for the lifetime of your process. Do **not** cut/paste *all* of the code in this answer into a single function you're expecting to call repeatedly. – WhozCraig Oct 08 '14 at 07:36
  • usually initialization calls once in programm – Maksym Oct 08 '14 at 07:38
3

If you're using C++ 11 you can consider using <random> header (you also have to seed the random engine):

#include <random>

std::random_device rd;
std::default_random_engine dre(rd());

std::uniform_int_distribution<int> uid(1,10);

int number = uid(dre);
w.b
  • 11,026
  • 5
  • 30
  • 49
0

1) You shouldn't use rand(), it has bad distribution, short period etc...

2) You shouldn't use %x when MaxValue % x != 0 because you mess your uniform distribution (suppose you don't use rand()), e.g. 32767 % 10 = 7 so number 0-7 are more likely to get

Watch this for more info: Going native 2013 - Stephan T. Lavavej - rand() Considered Harmful

You should use something like:

#include <random>

std::random_device rdev;
std::mt19937 rgen(rdev());
std::uniform_int_distribution<int> idist(0,10); //(inclusive, inclusive) 

I in my codes use something like this:

template <typename T>
T Math::randomFrom(const T min, const T max)
{
    static std::random_device rdev;
    static std::default_random_engine re(rdev());
    typedef typename std::conditional<
        std::is_floating_point<T>::value,
        std::uniform_real_distribution<T>,
        std::uniform_int_distribution<T>>::type dist_type;
    dist_type uni(min, max);
    return static_cast<T>(uni(re));
}

NOTE: the implementation is not thread safe and constructs a distribution for every call. That's inefficient. But you can modify it for your needs.

relaxxx
  • 7,566
  • 8
  • 37
  • 64
  • Re point 1: it depends on what you're doing. For serious analytic work (Monte Carlo evaluations and such), you shouldn't use `rand()`. For just playing around and games, `rand()` is generally sufficient, and a lot simpler. – James Kanze Oct 08 '14 at 08:58
0

Your problem is that you didn't initialize the seed for rand(), i.e. you didn't call srand() (in the usual old form of srand(time(NULL))), so you get the same number sequence.

But, anyway, you should not use rand() in C++11 and newer. You may want to use std::mt19937 and std::uniform_int_distribution instead.

You may want to read this answer for more details.
(I'm not going to duplicate the code and answer text here).

Community
  • 1
  • 1
Mr.C64
  • 41,637
  • 14
  • 86
  • 162