Alright I'm not sure if I worded the title correctly but I'm looking to generate a random number based on the numbers in an arraylist.
So if I have the values 1,2,12,6 I would like to generate a random number based on those numbers, so the return number from the random number generated would be either 1, 2, 12, or 6 and nothing else. Hope I explained that well enough, I apologise if not. Thanks guys.
3 1811
Ok after walking down to dinner it sort of blinged in my head that i could just count the arraylist then generate a random number and based on that value and i could reference the index and the value, thanks anyways!
So far what you tried ..please show us the code?
JosAH 11,448
Recognized Expert MVP @dmjpro
As the OP wrote: s/he found the answer: build an array { 1, 2, 12, 6 } and generate a random number in the range [0, array.length)
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: flupke |
last post by:
Hi,
i create my GUIs mainly via wxGlade. However when you start of to
program and want to do some rearranging to the gui, wxglade overwrites
your file and you've got to put your own code back in.
I think i can work around that (at least a bit) by making a second file
that imports the gui generated by wxglade and make classes that extend
the original ones.
For instance i could have a class MainForm that extends the wxFrame
|
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.
|
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.
|
by: abhi |
last post by:
How do you get random numbers in vb .net?
I want random six digit numbers.
Cheers,
Abhi
|
by: fieldfallow |
last post by:
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Assuming there isn't, as it appears from the docs that I have, is there
a better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.
| |
by: muttaa |
last post by:
Hi everybody,
May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?
Thanks in advance!
|
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...
|
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...
|
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;
|
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...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |