473,387 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating Random elements in an array

Basically I need to create array eleements for an array using rand (). However whenever the code that I provide, the array is filled with the same random array. I am thinking that I need to do somehting with the count++ to get it to repeat. Can someone please help me out?

Expand|Select|Wrap|Line Numbers
  1.  void RandArray (int mainArray[], int count)
  2.  {/*RandArray */
  3.     int minRange;    // the minimum value the user decides for the array
  4.     int    maxRange;    // the maximum values the suer decided for the array
  5.     int randNum;    // the random number generated 
  6.  
  7.  
  8.     // User sets MAX and MIN array values
  9.     cout << "Minimum Value for the Array values: " ;
  10.     cin >>  minRange;
  11.     cout << "Maximum Value for the Array values: " ;
  12.     cin >> maxRange;
  13.  
  14.     randNum = (minRange + rand() % maxRange);
  15.  
  16.     // main loop to iterate numbers
  17.     for (count = 0; count < ARRAY_SIZE; count++)
  18.     {
  19.         mainArray[count] =randNum;
  20.  
  21.     }
  22.  
  23.  
  24.  
  25.  }/* RandArray
For example if I set minRange = 10 and MaxRange = 40
the mainArray becomes {11,11,11,11,11,11,11,11,11,11,11,11,11,11,11}

Help please!
Shellina
Jul 16 '11 #1
2 4730
Ramsin
15
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main() {
  7.     srand(time(NULL));
  8.     int n = rand()%10 + 1;
  9.     int a[n];
  10.  
  11.     for(int i=0; i < 10; i++) {
  12.         a[i] = rand()%100 + 1;
  13.         cout << a[i] << endl;
  14.         i++;
  15.     }
  16. }
Jul 16 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tech | last post by:
I have a table tblEmails which contains a list of email ids. ID,address_id,list_name is the structure of it. I need to create a sub list for a particular list_name say "Google Customers" , then...
7
by: Micheal Artindale | last post by:
I am looking at creating list of letter combinations. letters a-h 6 letters per combination letter can repeat its self in the combination, but not next to its self and, a series of letter can...
1
by: Videotron | last post by:
Thanks to James A. Fortune in this Newsgroups "Creating random letter combinations", I execute the code he proposes and there were 500,000 combinations generated. This is more than we need to give...
5
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag =...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
2
swatmajor1
by: swatmajor1 | last post by:
I have been creating a basic sodoku game as a school assignment. Basically to load a puzzle, a pre-set arrangment of numbers are loaded into a hidden picture box array called "Picsolution". What I...
11
by: Jeff | last post by:
het gang. Happy Holidays and stuff. Here is what I am wanting to do, but need some guidance as hwo to start in the right direction. using sql db on ms server i have players sign up for a...
11
by: Matthew Wells | last post by:
Hello. I have figured out how to create an instance of an object only knowing the type by string. string sName = "MyClassName"; Type t = Type.GetType(sName); Object objNew =...
2
by: oswald.harry | last post by:
hi i am reading a set of jpeg files(RGB) and extracts the pixel values as longs.i want to create a 2d array with numof rows=numof images and numof cols=numof pixels in each image.ie each row of...
0
by: Timothy Grant | last post by:
On Mon, Oct 13, 2008 at 2:29 PM, aditya shukla <adityashukla1983@gmail.comwrote: I would start here http://docs.python.org/library/random.html you are likely interested in choice or sample. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.