random number generator

This is a discussion on random number generator within the C++ Programming forums, part of the General Programming Boards category; I have an array int arr[10]; I want to fill the array up with random numbers between int low = ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    27

    random number generator

    I have an array int arr[10];

    I want to fill the array up with random numbers between

    int low = 5;
    int high = 15;

    How would I do this? From what I know, I think I need the <cstdlib> and I need some kind of rand function. But thats all I know. Can anybody help? Thanks in advance!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Yes, read this tutorial on using rand. After that, it's pretty easy.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It depends on whether you are allowed to have duplicates or not. If you want 5, 6, 7, 8, 9, 10, 11, 12, 13, and 14 in some random order in the array, then you should put them into the array in order using a for loop, then use random_shuffle to shuffle the values in the array. If you just want 10 random numbers from 5 to 15, then use a loop to get 10 random numbers and store them in the array.

    To get a single random number from 5 to 15, look in the FAQ for the proper formula to use with rand(). Prelude's tutorial is probably unnecessarily complex for this problem.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by Daved
    It depends on whether you are allowed to have duplicates or not.
    If you force non-repeating numbers, the numbers aren't random because each number relies on the numbers that came before...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    27
    I read the website given and I still can't figure it out.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    - Declare the array to be 10 integers.
    - Seed rand.
    - Call rand 10 times but make sure that the value you want is in the range 5 to 15.
    - put what rand returns in the array each time.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    If you force non-repeating numbers, the numbers aren't random because each number relies on the numbers that came before...
    It depends on the definition of randomness and doesnt really matter since the pseudo-random numbers depend on the seed, anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> I read the website given and I still can't figure it out.
    Did you read the FAQ on this website? Have you tried coding anything yet? Did you code the declaration of the array? How about the for loop that sets 10 numbers? Have you figured out that you need to call srand() once at the start of the program? Have you figured out the proper way to do that? If you have any of that, show us what you've coded.

    The hard part for you will probably be making the result from rand() fit in between 5 and 15, but you should get the other stuff done first and make sure itis working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 05:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM

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