473,804 Members | 3,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Urgent] Need help, unique random numbers.

4 New Member
Hello,

I need help generating 50 unique numbers using rand() BETWEEN 1 and 100 and assign them to a array. I have worked out the number randomization and assignment, but I cant figure out how to generate unique numbers. Can anyone modify this code so that it generates or rather assigns only the unique number to the array:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstring>

using namespace std;

int main()
{
int num,anum[50];
srand ((int)time(NULL ));

for (int i=1; i<=50; i++) {
num = ( 1 + rand() % 100);
anum[i] = num;
cout << setw(5) << anum[i];
if (i % 5 == 0) {
cout << endl;
}
}
return 0;
}

Any help is greatly appreciated.

thanks
Dec 15 '06 #1
3 2206
Ganon11
3,652 Recognized Expert Specialist
Once you have generated the random number, you can have a second for loop (with index j) from 0 to i - 1, checking each previously assigned element of the array. If the element at array[j] equals the element you just assigned (array[i]), then subtract 1 from i (so that the ith element will be randomly assigned again) and break from the second for loop.
Dec 15 '06 #2
ronnchpra
4 New Member
Once you have generated the random number, you can have a second for loop (with index j) from 0 to i - 1, checking each previously assigned element of the array. If the element at array[j] equals the element you just assigned (array[i]), then subtract 1 from i (so that the ith element will be randomly assigned again) and break from the second for loop.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstring>

using namespace std;

int main()
{
int num,anum[50];
srand ((int)time(NULL ));


for (int i=1; i<=50; i++) {
num = ( 1 + rand() % 100);
anum[i] = num;

for (int j=1; j<=-1; j++) {
if (anum[j] == anum[i]) {
i = i-1;
break;
}
}

cout << setw(5) << anum[i];
if (i % 5 == 0) {
cout << endl;
}
}
return 0;
}

just tried that man, still no resolve, it still for some reason assigns same numbers to 2 or more array
Dec 15 '06 #3
ronnchpra
4 New Member
Hmm wait a min, I think I read your response wrong.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstring>

using namespace std;

int main()
{
int num,anum[50];
srand ((int)time(NULL ));

for (int i=1; i<=50; i++) {
num = ( 1 + rand() % 100);
anum[i] = num;

for (int j=0; j<=i-1; j++) {
if (anum[j] == anum[i]) {
i = i-1;
break;
}
}

}

for (int z=1; z<=50; z++) {
cout << setw(5) << anum[z];
if (z % 5 == 0) {
cout << endl;
}

}
return 0;
}

WORKED LIKE A CHARM.... thanks a lot man.. thanks a lot
Dec 15 '06 #4

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

Similar topics

5
11324
by: lallous | last post by:
Hello, This code works fine when 'size' is less than 32768 however when size is bigger this function never returns. Can't find out why?! If I break into the code I can see that 'i' is 32768.... void MakeRandomArray(unsigned long **a, unsigned long size) { unsigned long *data = new unsigned long ; double sizef = (double)(size - 1);
0
749
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. #include <iostream> #include <fstream>//for declaring objects of the ofstream and ifstream #include <time.h> #include <stdlib.h> using namespace std;
1
1018
by: Chuck Insight | last post by:
Hi Everyone, I have a VB script (.aspx) project that needs to produce nine unique, randomly generated numbers that uses all nine of the numbers. I will greatly appreciate either a recommendation of where to look on the web for my answer, or your direct response. Thanks a 1,000,000 for your ideas. Chuck
1
1942
by: Wally | last post by:
I am after VB.net code for a random number generator that will generate 10 different numbers between 1 and 10 and put them into an array. For example I want the output to be something like 10,7,8,6,9,4,5,2,3,1. Can anyone provide me with the code? Thanks,
6
2729
by: kossanah | last post by:
I like to seek to your assistance in any measure.I need your help on how to go about This: I am developing a promotional site where user will be issued a ticket(manually) which will be a unique 10 digit alpha-numeric digits.The ticket will be issued out manually at a Company sales point once they purchase at the store. This is intended for this season's promotion. The user will use the Ticket no. to fill out a form provided online. ...
1
1706
by: ronnchpra | last post by:
Hello, Im still kind a new to C++ Programming? Can anyone help me write a small code that would generate 50 random numbers, say from between 1-100? This is a small part of a program I'm writing. any help is greatly appreciated. Thanks in advance.
4
1645
by: UMstudent | last post by:
I'm working on this program for class where I have to an amount of numbers that are randomly picked from 0 - 50. So what I have done is say there are 20 slots in my array so I'm trying to generator 20 random numbers for the array between 0-50 however I've ran into a few problems. Mainly figuring out how to make it print 20 seperate completely random numbers into the array. So here is what I have any help will be greatly appreciated. Thank you. ...
17
13508
by: Xoomer | last post by:
Hi, Can some post a code with simple Unique Random Numbers. Random numbers stored in arr but unique.
1
2107
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions. In this initial phase of the...
0
9582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.