Thread: Array and random number

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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, 09:26 PM
  3. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04: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