Thread: Random Numbers

  1. #1
    Unregistered
    Guest

    Question Random Numbers

    Hi.

    Before you say anything, I have read the FAQ, but I am very new to coding (atleast C++) and it didn't help much.

    How do I make a random number, say.... a number from 1-5??

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    2

    there you go.

    wait

    5

    no no.

    1

    feel free to use me as your random number generator anytime.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    seriously.
    srand(a_number) function will seed your number generator.
    then use rand() anytime you need a random number.

    to get it between two numbers try this: I pulled it from a book but have not tried it.

    random_number = (rand() % (max + 1 - min) + min);
    Last edited by taylorguitarman; 09-16-2001 at 11:43 PM.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    it works.

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    but this gives the same "random" number each time the program is executed. How is it done so it gives a new number each time?

  6. #6
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    for a start:
    srand( (unsigned)time(0) );

    that uses time as you're "random" seed thus making the seed number different each time. If you need truly random numbers you'll have to read up on the topic.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Make sure you use srand to seed the rand function. srand is only called once (don't include it in a loop, for example). Rand() can be in a loop though. You can enter a number for srand, but that'll result in the same random numbers if the same number is entered. Seed with the time function in <ctime>.
    Code:
    srand(time(0));
    .
    .
    .
    x = y + rand() % z;
    y is the number you want to start the random numbers with, and z is the range of numbers you want them to fall into. E.g., To write one dice being cast:
    Code:
    num = 1 + rand() % 6;
    The die starts at one, and can be any of six numbers, 1 thru 6.

  8. #8
    Unregistered
    Guest
    num = 1 + rand() % 6;

  9. #9
    Unregistered
    Guest
    num = 1 + rand() % 6;
    If you're new to coding, it's very important that you understand how this statement works. The % in the above code line is referred to as the modulus operator. The modulus operator gives what is more coloquially known as the remainder of a division. rand() supplies a random number between 0 and 32767 or something similar (it varies by compiler). When you take that number and modulus by 6, you get a remainder, which must be a number smaller than 6. Example:

    1000 % 6 = 4
    because:
    1000 / 6 = 996 R 4

    This will give you a range between 0 and 5. To make it 1 through 6 we add 1 to the equation.

  10. #10
    Unregistered
    Guest
    This is my code...
    Code:
    #include <iostream.h>
    
    srand();
    
    int main()
    {
      int number;
      number=1+rand()%6;
      cout<<number;
      return 0;
    }
    That always gets the number 5... How do I fix that??

  11. #11
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    See the above posts, you need to seed the rand function.

    #include <ctime>

    //In main - you can't call functions globally

    srand( (unsigned)time(0) );

  12. #12
    Unregistered
    Guest

    Cool

    Yay!! It works.

    Thank-you all for your help, I'm very new to C++.

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Also, if you want your random number to be between 1 and 5, per your first post, change ...rand() % 6 to ...rand() % 5.

  14. #14
    Unregistered
    Guest

    Red face

    You people are so gay, here goes the easy way.

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

    void main()
    {
    clrscr();
    randomize(); //initiates the random function
    int x=random(5); // generates a random number b/w 1-5
    cout << x;
    getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM