473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

generating random numbers for 1,000,000 elements

9 New Member
In my code I want to be able to generate up to 1000000 numbers. Will my random number generator do this without crashing? If not, can someone assist me in what I need to do?

thank you

Expand|Select|Wrap|Line Numbers
  1. int main()
  2.     int num,i,elem, percent;
  3.     a[num];
  4.     cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort[THEN PRESS ENTER]:"<<endl;
  5.     cin>>num;
  6.     cout<<"Please Enter THE PERCENTAGE you want to sort[THEN PRESS ENTER]:"<<endl;
  7.     cin>>percent;
  8.     for (i=1; i<=num; i++)
  9.     {
  10.         elem =((rand()*9)+1);
  11.         cout<<endl<<"element "<<i<<"is "<<elem<<endl;
  12.         a[i]= elem;
  13.         cout<<"When i is "<<i<<" ai is "<<a[i];
  14.     }
  15.     merge_sort(1,num);
  16.     cout<<endl<<"So, the sorted list (using MERGE SORT) will be : "<<endl;
  17.     for(i=1;i<=num;i++)
  18.     {
  19.          cout<<a[i]<<" ";
  20.     }
  21.     cout<<endl<<"Time Elapsed is: "<<timeElapsed<<endl<<endl<<endl<<endl<<endl;
  22.     system("PAUSE");
  23. return 0;
  24. }
Oct 31 '07 #1
4 2503
Ganon11
3,652 Recognized Expert Specialist
As long as you don't ask for more than INT_MAX numbers, you should be fine.
Oct 31 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your array of random numbers is on the stack. You might find that you crash. If so, create your array on the heap.
Oct 31 '07 #3
mqueene7
9 New Member
Your array of random numbers is on the stack. You might find that you crash. If so, create your array on the heap.

How do i create it on the heap?
Oct 31 '07 #4
RRick
463 Recognized Expert Contributor
Since num is the number of entries, to create an int array of num entries:
Expand|Select|Wrap|Line Numbers
  1. int *iarray = new int[ num];
  2.  
  3. //  Also, what you new, you must delete
  4. delete [] iarray;
  5.  
Its not a good idea to multiply the random number, because the set of random numbers can cover all positive values of int. Line #11, might cause an integer overflow. Once that happens, your random number sequence might have repeat values.
Oct 31 '07 #5

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

Similar topics

4
by: Keith Griffiths | last post by:
I'm trying to do a search under a set criteria followed by a selection of random entries meeting this criteria. But I don't seem to be able to achieve this. The idea being to search on say...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
6
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
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 =...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
7
by: bipi | last post by:
Dear all, I found function rand(), it can create random number but this function can not define the range of number which I want to get it, such as, I want to get random number in the range from...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.