3

How do I create a program that generates ten random numbers from 1 -> RAND_MAX?

RAND_MAX must be a number input by the user.

#include <iostream>
#include <stdlib.h>

int main()
{
    using namespace std;
    int x;
    int y;


    Random:
    {
        x = rand();
        cout << x << endl;
    }

    y = y + 1;
    if (y == 10) {
        return 0;
    }

    goto Random;
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Lemonizer
  • 83
  • 2
  • 4
  • 14
  • Sorry, but the program wouldn't appear, it should be there now, tough. – Lemonizer Mar 01 '13 at 14:35
  • 6
    RAND_MAX is defined by std library http://en.cppreference.com/w/cpp/numeric/random/RAND_MAX –  Mar 01 '13 at 14:44
  • possible duplicate of [Random number c++ in some range](http://stackoverflow.com/questions/7560114/random-number-c-in-some-range) – Raymond Chen Mar 01 '13 at 14:53
  • 1
    The question has already been answered, but I'd like to point out you need to initialise y otherwise you may find your program looping infinitely :) – Ben Hymers Mar 01 '13 at 14:57
  • @BenHymers I think that has already been done, (I think!). It only generates ten numbers, but they are too big. my if statement does that, I am pretty sure of it. – Lemonizer Mar 01 '13 at 15:06
  • @Lemonizer y is assigned to (on the line `y = y + 1;`) but it's using its own value in that expression, and before that line is reached, it hasn't been initialised. The line `int y;` will probably set y to 0 in debug builds, but you can't rely on that. Run this in release and it will probably contain garbage, almost certainly more than the 10 that you're checking for, so you'll loop forever (or until y overflows!). You probably meant to write `int y = 0;`. Note that x is ok since you're assigning the result of `rand()` to it. Hope that makes sense! – Ben Hymers Mar 04 '13 at 08:54
  • 1
    possible duplicate of [Generate Random numbers uniformly over entire range](http://stackoverflow.com/questions/288739/generate-random-numbers-uniformly-over-entire-range) – joce Apr 13 '13 at 00:41

1 Answers1

5

Disclaimer: rand is a quick and dirty way to generate random numbers, as it may not generate numbers perfectly uniformly and you'll run into some issues if RAND_MAX (the upper limit for rand) is defined to be smaller than your target range. In modern C++ it would be better to use the <random> header, as per the question Generate random numbers uniformly over an entire range.


Something like:

int main()
{
  int randMax;
  cin >> randMax;
  for (int y = 0; y < 10; y++)
  {
    int x = rand() % randMax; // Range = [0, randMax)
    cout << x+1 << endl; // Range = [1, randMax]
  }
}

Oh, and do try to avoid goto (at least in my opinion). Here are two questions about it.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • Thank you! But where exactly do I put this, and should it replace something? Also why can I not use "goto" ? ---Edit--- My only programming experience is with a TI-84, and I use goto all the time there, RandInt() is what its called on the TI Calculators are so much simpler :P – Lemonizer Mar 01 '13 at 14:43
  • And don't ever use capital letters for local variables. – Hans Passant Mar 01 '13 at 14:43
  • this will not compile `RAND_MAX` is defined in the same header as `rand` and will be substituted by a number –  Mar 01 '13 at 14:45
  • Hmm, thank you for all your replies, but I don't see how the process can repeat ten times without goto. – Lemonizer Mar 01 '13 at 14:51
  • @Lemonizer This code basically replaces everything. The for-loop causes it to repeat 10 times. – Bernhard Barker Mar 01 '13 at 14:54
  • Thank you! Well I have to say, generating random numbers was far more complicated than i thought. I looked at other two questions, but did not understand a thing. I tried to take a step away from my tutorial and try to duplicate some of the programs on my TI-84, but as said, it is very hard. I think I will return to my tutorial now. Thank you everybody, if I ever make a game, I will name characters after you! – Lemonizer Mar 01 '13 at 15:00
  • @chux Added a disclaimer. – Bernhard Barker Aug 01 '21 at 03:57