473,782 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating a Random Number

16 New Member
Hi,

I need a piece of code to generate a random number between 0 and 5 (both exclusive) ie., number should be either 1,2,3 or 4. And also I need only two unique random numbers out of the four numbers. ie., out of 1,2,3 and 4 only two numbers should be picked randomly and those two numbers should not be repeated. Any help please....

Regards,
VRK
Jan 13 '07 #1
6 2622
r035198x
13,262 MVP
Hi,

I need a piece of code to generate a random number between 0 and 5 (both exclusive) ie., number should be either 1,2,3 or 4. And also I need only two unique random numbers out of the four numbers. ie., out of 1,2,3 and 4 only two numbers should be picked randomly and those two numbers should not be repeated. Any help please....

Regards,
VRK
Did you manage to use Math.random() as was suggested here?
Jan 13 '07 #2
vrkamalakar
16 New Member
Hi,

I got it....Thanks a lot...


Did you manage to use Math.random() as was suggested here?
Jan 13 '07 #3
r035198x
13,262 MVP
Hi,

I got it....Thanks a lot...
All you need is to get the first number, then use a while loop that generates another number until the generated number is different from the first number.
Jan 13 '07 #4
Ganon11
3,652 Recognized Expert Specialist
Alternatively, you could use an ArrayList (found in java.util.Array List) to build a short list of integers (from 1 to 4, in your case). Then, using a random number generator, generate a number between 0 and the size of your ArrayList, select that Integer from the List, and remove the Integer object. This way, you are guaranteed never to produce the same Integer, as it will be removed after you use it.

For a larger application, this is wonderful, but for 4 possible numbers, it may take too much time, memory space, coding, etc.
Jan 13 '07 #5
vrkamalakar
16 New Member
Hi,
Can you give me a piece of code for that. With the earlier method Iam getting even 0 as a random number which is not my requirement. I should get either 1,2 3 or 4.


Alternatively, you could use an ArrayList (found in java.util.Array List) to build a short list of integers (from 1 to 4, in your case). Then, using a random number generator, generate a number between 0 and the size of your ArrayList, select that Integer from the List, and remove the Integer object. This way, you are guaranteed never to produce the same Integer, as it will be removed after you use it.

For a larger application, this is wonderful, but for 4 possible numbers, it may take too much time, memory space, coding, etc.
Jan 14 '07 #6
vrkamalakar
16 New Member
Hi,
Thanks....I got it.....


Hi,
Can you give me a piece of code for that. With the earlier method Iam getting even 0 as a random number which is not my requirement. I should get either 1,2 3 or 4.
Jan 14 '07 #7

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

Similar topics

7
7290
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? STL set, map? Could you give me a little code example? Thank you.
16
12079
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 numbers using asp.net? I know about the random namespace within .net, but I need a reference to some code that do the similar stated function above. Plus If you have any coding practice ideas for the above defined project please share them.
6
5914
by: Intiha | last post by:
Hello all, I am trying to generate random seeds for my simulations. currently i was using srand(time(NULL); for this purpose. But for confidence in my results i ran it using a script in a loop. Since the time b/w execution is very similar, many simulation runs resulted in exact same results. Is there a better way of seeding the random number generator in c/c++
2
2776
by: Simon Wittber | last post by:
I'm building a web application using sqlalchemy in my db layer. Some of the tables require single integer primary keys which might be exposed in some parts of the web interface. If users can guess the next key in a sequence, it might be possible for them to 'game' or manipulate the system in unexpected ways. I want to avoid this by generating a random key for each row ID, and have decided to use the same approach for all my single key...
2
2741
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns out
1
2676
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 using rand() method to print unique random number. And finally i want to know, If a program generate random numbers and the same program will execute after 100 or some days, it will not to generate the old random number (i.e the random number...
14
4753
by: avanti | last post by:
Hi, I need to generate random alphanumeric password strings for the users in my application using Javascript. Are there any links that will have pointers on the same? Thanks, Avanti
5
8376
by: dav3 | last post by:
Howdy folks, was wondering if anyone could assist me in a better method for generating a alphanumeric sequence of 6 characters/digits. The method I am currently using gives me a random license plate number for a project I am working on. But I am unsatisfied with the results I am getting. Here is my current method, it works but like I said there has to be a better way of doing it. public static String plate() { int x = 6; char plate...
0
26045
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. The key point is that you need to start the random number generator with a seed. Doing it in the form load with the number of milliseconds since midnight is an easy way to start. The VB.Net version This demo project will be as simple as I can...
26
7915
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()); int r2 = rand(); bool f = r1 == r2;
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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
10311
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...
0
8967
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
7492
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
5378
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
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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 we have to send another system
3
2874
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.