Easy Programming – Beginner C++ Tutorial – Random Number Generator (11)

37
21



This is another new thing that you’ll learn in C++ today. Today I show you how to create a program that acts as a random number generator and gives you a random number based on what the user inputs as the maximum value for the random number. So whether the number is 5 or 5,000,000, the program will be able to give you a random value. This is great practice for beginners with C++ and this tutorial should help further your knowledge about the basics of C++.

For help with the looping, please check out my other video located here:

In that video, I explain exactly how the “for” loop works. It’s very simple and it’s just a few minutes.

Learn how to do this in JavaScript:

And remember to visit my site to view my other programming tutorials! They are all on the website and it will be updated as soon as new programs are uploaded.

I hope you enjoy the video and if you have any requests feel free to let me know. Thanks for watching and remember to subscribe!

#EasyProgramming #C++

———–
Visit my website:
Subscribe on YouTube:
Support me on Patreon:
Follow me on Twitter:
Like me on Facebook:
Check out my Github:

Nguồn:https://dothihoa.com/

37 COMMENTS

  1. I keep getting duplicate values when I run the program. I am trying to use a random function for a lottery generator. My professor wants me to use a random function. I seeded the function, but still getting duplicate values. Any suggestions??

  2. How to add random numbers that does not require a 50 variable but like 2 to 4 variables that repeat to equal to the total amount with two separate totals such as players to see who wins? Also, please tell how to reset values to 0 when they are too high?

  3. I hate C++ so much.

    Getting a random number is other languages is really easy to do, and it took me weeks just to find this tutorial that gave me a legit random number instead of a random number that is actually always the same every time.

  4. sir i need this program plzz do it and sent me thank u
    The Game of Catching Numbers
    Childers are more attracted towards computer games rather than studying mathematics. So to teach children’s of primary school, our motive is to design a game such that during their enjoyment they also learn counting numbers
    The following is the working example:
    Suppose below picture is your computer screen. The minimum following requirements must be met by your application program for Children. Extra feature will be appreciated. All the said requirements are compulsory.
     First number (object) is start with zero at the corner of the screen having screen coordinates (0, 0).  For the second number generate random number from 1 to 100 and also random coordinates.  Ten points are generated randomly as false number. If you touch those number, you will lose.(You can use array of point objects)  Now Children use the arrows key to move first number left, up, down right to reach 2nd number.  When first number reach the co-ordinates of 2nd number. Add both numbers. The resultant value now become the first number and again generate 2nd number as in step 2.  The Game will end when children complete adding 500 points.  For each addition provide a time on with which you can add numbers. If time expires and you did not reach the co-ordinates then you lose. You cannot go back that co-ordinate from where you just came.
    Use the concepts of class Point we discuss in the last two lectures, objects, for the fulfilment of this assignment.

    0
    10
    Total Score:
    Time Left:
    0
    0
    0

    Your .h should contain these functionality to fulfil above Requirements

    This is only for your guide you may change the functions, add or remove functions according to your understanding . Class Point { Private: Double x; Double y; Int Value;

    Public: Point()//Default constructor Point(Double,Double,int)//Three Argument constructors;

    Void Shift(Point P)// Shift the first point when user press keys

    Void Initialize(Double, Double, int)

    Void generateRandomCordinates();

    Void SetValue(int Value);

    Int GetValue() Const;

    Void Setx();

    Double getX()Const

    Void Sety();

    Double gety()Const

    Void AddPointValue(Point P2);//This function add the TWO points value when your first point successfully reach on second true point co-ordinates

    Void DisplayPoint();// This will use to display value of a point on screen

    Bool CheckCorrdinates(Point p);

    Bool CheckTime();//check time remaining

    };

  5. Okay.. this is no way generates a "random" number. After trying it last night, it clearly does not generate "random" numbers over successive iterations.

  6. thanks man you explained it very well and specially hats off to to the algo (rand() % max )+1 works as charm once again thanks for sharing such an easy algo

  7. my range is 1-100, and they keep going up, but when I get to 100 it resets. This doesn't feel random… what am I doing wrong? Here's a list of what I got.
    7, 14, 28, 32, 48, 63, 72, 72, 84, 100, 4, etc.

  8. Hi I'm trying to get my code to generate a letter i know the code but i keep getting the same letter unless i exit the program. How can i get a new letter without having to exit and get back in?

  9. Man I learned a lot! However, I think you should try to talk a little slower some of us don't know all the concepts and need some time to assimilate.

  10. im not exactly doing a generator its a gues d num game but i put the randomization code but d answer is always d same num some1 please help

  11. Write a C++ program that will implement a quick pick gambling system for different gaming systems in Belize. The games are for Daily Boledo, Sunday Lottery Jackpot, Mek mi rich and Fantasy 5. Each of these games are to be implemented in a function, where you would write random functions to randomly generate numbers for a quick pick ticket for a customer. The program should start up asking the user to enter a selection for a game: Press 1 for Daily Boledo, Press 2 for Sunday Lottery Jackpot, press 3 for Mek mi Rich, 4 for Fantasy 5 and 0 as the sentinel. 

    The function for boledo should generate and display a random number from 0 to 99 OR two sets of single digit numbers from 0 to 9.

    The function for lotteryJackpot should generate and display random number from 0000 to 9999 OR four sets of single digit numbers from 0 to 9. 

    The mekMiRich function should generate and display first a set of 4 different random numbers from 1 to 40 and then a set of 1 number from 1 to 10. 

    Lastly, the fantasyFive function should generate and d

  12. when i tried debugging, a message popped up saying "unable to start program. access is denied." Is anyone able to tell me why it didnt run properly?

  13. how do I do this
     "seed" the random number generator
      compute and store the computer's card's value (2-14) using the random number generator
      output the computer's card's value, outputing 2-10 as that value, 11 as Jack, 12 as Queen, 13 as King, and 14 as Ace

  14. I am trying to make a program that randomly picks a one of the available falls 30 times. Then it will show the score. I dont know whats going wrong but it picks the falls likes 1×3 then 2×3 and then 3×2 and likes this and when it reaches 10×3 it shows my score. and it does the loop from 1×3 to 10×3 likes 10 times. Im sorry about my bad english and im noob in coding.

    Ps. Im from finland so dont be confused about my Oikein, Vaarin and things likes that, its just my language.

    #include <iostream>
    #include <Windows.h>
    #include <ctime>
    #include <cstdlib>
    #include <cmath>

    using namespace std;

    int main ()
    {

    int max, random_number;
    max = 20;

    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
    int g;
    int h;
    int u;
    int j;

    int k;
    int l;
    int m;
    int n;
    int o;
    int p;
    int q;
    int r;
    int s;
    int t;

    int looper;

    int score;

    char valinta ='å';

    cout <<"Kertolaksu Peli!nnPeli alkaa kun valitset laskut.nJos haluat kolmen kertotailua kirjoita 1 ja paina enter. nJos haluat neljan kertotaulua kirjoita 2 ja paina enter.nJos haluat kolmen ja neljan kertotaulua kirjoita 3 ja paina enter.nn";
    cin >>valinta;

    if ( valinta == 1 )

    Sleep (10);
    cout <<"Valitsit kolmen kertotaulun!nPeli alkaa!";
    Sleep (3000);
    cout <<"nn5!n";
    Sleep (1000);
    cout <<"4!n";
    Sleep (1000);
    cout <<"3!n";
    Sleep (1000);
    cout <<"2!n";
    Sleep (1000);
    cout <<"1!n";
    Sleep (1000);
    cout <<"Alkaa!nn";
    Beep (540, 1500);

    system("cls");

    looper = 1;
    while ( looper <= 30 ) {

    srand(time(0));
    random_number = (rand () % max) + 1;

    if ( random_number == 1 || 11)
    {
    cout <<"1x3nn";
    cin >> a;
    }
    if ( a == 3 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 2 || 12 )
    {
    cout <<"2x3nn";
    cin >> b;
    }
    if ( b == 6 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 3 || 13 )
    {
    cout <<"3x3nn";
    cin >> c;
    }
    if ( c == 9 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 4 || 14 )
    {
    cout <<"4x3nn";
    cin >> d;
    }
    if ( d == 12 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 5 || 15 )
    {
    cout <<"5x3nn";
    cin >> e;
    }
    if ( e == 15 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 6 || 16 )
    {
    cout <<"6x3nn";
    cin >> f;
    }
    if ( f == 18 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 7 || 17 )
    {
    cout <<"7x3nn";
    cin >> g;
    }
    if ( g == 21 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 8 || 18 )
    {
    cout <<"8x3nn";
    cin >> h;
    }
    if ( h == 24 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 9 || 19 )
    {
    cout <<"9x3nn";
    cin >> u;
    }
    if ( u == 27 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    if ( random_number == 10 || 20 )
    {
    cout <<"10x3nn";
    cin >> j;
    }
    if ( j == 30 )
    {
    cout << "Okein!nn";
    score = score + 1;
    }

    else
    {
    cout <<"Vaarinnn";
    score = score + 0;
    }

    looper = looper + 1;
    cout <<"Your final score is "<< score << endl;

    }
    return 0;
    }

  15. hey you can use this Statement in your program to generate random numbers between 15 to 150 or in any range…!
    random_number = (rand () % (Upper_limit – Lower_limit)) + Lower_limit ;

  16. Can you help me with this question?

    "
    Write a program, using a for loop, to generate 10 random numbers between 15 and 150, inclusive. Then, the program should print how many of those numbers fall into the following categories: (1) between 15 and 50, inclusive; (2) between 51 and 100, inclusive, and (3) between 101 and 150, inclusive. Assume there is no user input for this program. "

LEAVE A REPLY

Please enter your comment!
Please enter your name here