random numbers

This is a discussion on random numbers within the C++ Programming forums, part of the General Programming Boards category; what is the easiest way to get a set of random numbers and set them as a variable?...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    random numbers

    what is the easiest way to get a set of random numbers and set them as a variable?
    im new

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    You can use the randamize function... Function rand(100).. So random numbers within 100 . TO store them into variables..


    a=rand(100).. You have to include the file math.h

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> Function rand(100)..

    You may be able to on some compilers, but it is not standard.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use an array and a loop.....
    get 10 random numbers between 1 and 40 ...
    Code:
    #include <iostream>
    #include<cstdlib>
    #include <ctime>
    using namespace std;
    int main ()
    {
    srand((unsigned)time(NULL)); // seed random number generator with system time
    int MyRand[10]={0}; // an array to store our random values;
    for(int i=0;i<10;++i)
    {
    MyRand[i]=rand()%40+1; // get random number between 1 and 40 and store in array
    cout<<MyRand[i]<<endl;
    }
    return 0;
    }
    Last edited by Stoned_Coder; 02-27-2002 at 08:59 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    1
    This is a very simple task my friend....

    Somewhere in your program perferibly at the beginning have
    srand(clock());

    to set the random seed.

    and to assign a variable to be a random number from whatever
    say... from 1 to 100.

    num = rand()%99+1;

    thats it


    Thank you have a nice day.

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, 09: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-12-2003, 12:03 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21