Array and random number

This is a discussion on Array and random number within the C++ Programming forums, part of the General Programming Boards category; may i noe how do i initialise a random number to array? is this how i do it?? Code: int ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    10

    Array and random number

    may i noe how do i initialise a random number to array?
    is this how i do it??
    Code:
     int result[3]={rand( ) % 9 + 1};

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    No. Either use a loop, and set the value of each element individually, or look up the generate() or generate_n() algorithms in the standard C++ library.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    how do i use looping for that? any example coding??

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Random numbers

    Code:
    The function
    Code:
    srand()
    is used to seed the random sequence. The
    rand()
    function shall compute a sequence of pseudo-random integers in the range [0, {RAND_MAX}] with a period of at least 2^32. Read more about
    C Programming Language .
    in the program just change/add each random number to the array.
    C Program Examples: C Program to generate random numbers.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    Quote Originally Posted by Silsilay View Post
    how do i use looping for that? any example coding??
    Any basic text or online tutorial on C or C++ will include examples of loop constructs within the first couple of chapters. Go, find any book or tutorial, and read. You are exhibiting unnecessary laziness if you expect answers to such trivial questions in forums.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    100
    Think about how to initialize a three-element array without using random numbers. Like if you wanted the array to be set equal to 1, 2, 3.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-16-2012, 04:42 AM
  2. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  3. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 03:44 PM
  4. fill array with random number
    By azsquall in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2008, 12:27 PM
  5. Random Number Array
    By thestrap in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 12:44 AM

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