473,769 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need suggestion about random selection

Hello.

Problem:
How can I select K random values from the elements 0,1,2,3,4...,N-1 ?

I don't know if there's an easy way of doing this, but here are suggestions
for doing it.

One way is to select K random elements (using a rand()% N), and then see if
any number was choosen twice, and then re-select the duplicats until the
choosen numbers are distinct. That ought to work fine when K much smaller
than N. But I guess it will be more problematic when K is about N/2. (when
K>N/2, just select the numbers that are not selected).

Another foolproof method (but probably very error prone when coding) is to
select one element, then generate a rand()% (N-1) and select the number
that is that many steps away from the last selected number (wrapping back
to 0 when reaching past N-1. Fine, you have two numbers, then select the
number that is rand()%(N-2) steps away, avoiding the already taken numbers,
and so on.... that way you will never get a number twice! The problem is
implementing it..

The third option is to use a vector and removing each choosen number, but
is that really efficient?

Do you have any clever idea for how to do this?
Jul 22 '05 #1
3 1810
Gunnar wrote:

Hello.

Problem:
How can I select K random values from the elements 0,1,2,3,4...,N-1 ?

I don't know if there's an easy way of doing this, but here are suggestions
for doing it.

One way is to select K random elements (using a rand()% N), and then see if
any number was choosen twice, and then re-select the duplicats until the
choosen numbers are distinct. That ought to work fine when K much smaller
than N. But I guess it will be more problematic when K is about N/2. (when
K>N/2, just select the numbers that are not selected).

Another foolproof method (but probably very error prone when coding) is to
select one element, then generate a rand()% (N-1) and select the number
that is that many steps away from the last selected number (wrapping back
to 0 when reaching past N-1. Fine, you have two numbers, then select the
number that is rand()%(N-2) steps away, avoiding the already taken numbers,
and so on.... that way you will never get a number twice! The problem is
implementing it..

The third option is to use a vector and removing each choosen number, but
is that really efficient?

Do you have any clever idea for how to do this?


Think about the following:
Take your vector
0,1,2,3,4...,N-1
Now shuffle it
3,4,0,N-1...,2,1
and take the first K elements

Look up <algorithm>. There alredy is a shuffle
algorithm ready to use (random_shuffle )
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
>> Do you have any clever idea for how to do this?
You had one!
Thank you very much!
Think about the following:
Take your vector
0,1,2,3,4...,N-1
Now shuffle it
3,4,0,N-1...,2,1
and take the first K elements

Look up <algorithm>. There alredy is a shuffle
algorithm ready to use (random_shuffle )

Jul 22 '05 #3
Gunnar wrote:
Do you have any clever idea for how to do this?

You had one!


Not really. If you are playing cards you have done the
exact same thing a thousend times before. You simply
didn't notice :-)
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6291
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 subject and then select a random set of records meeting that subject. Any ideas or thought would be helpful. I'm using Access XP to try this out. TIA
9
2257
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after each run and then re-counting the list and doing it all over again. My boss thinks that 'b' is somehow less fair than 'a', but the only thing I see wrong with it is that it is really inefficient and ugly as it's doing manually what 'a' does...
3
8831
by: Jennie Friesen | last post by:
Hello-- I would like to display a different line of text (32 different messages) on refresh, BUT with no repeats. The script I am currently using is: <script language=javascript type=text/javascript> <!-- a = Math.floor(Math.random() * 32);
4
2308
by: Marquisha | last post by:
If this is off-topic, please forgive me. But I thought this might be the perfect spot to get some advice about how to proceed with a project. Working on a Web site design for a nonprofit organization, and I'm donating the work. Haven't done Web work in a while, and things have changed since I last did anything of this magnitude. I'm looking for a solution that will enable me to edit pages easily and in totally WYSIWYG fashion, while...
4
2700
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
1
1547
by: Micak | last post by:
I'm using radnom selection in ms access to select certain number of questions for the exam. i have two versions of the report: - one without marked answers - one with marked answers the problem is when i generate the reports one after another, i don't get the same selection of questions. Does anyone knows how to generate two parallel reports that would give the same random selection?
8
3826
by: Kari Lavikka | last post by:
Hi! I have to select a random row from a table where primary key isn't continuous (some rows have been deleted). Postgres just seems to do something strange with my method. -- -- Use the order by desc limit 1 -trick to get maximum value -- CREATE OR REPLACE FUNCTION max_uid() RETURNS int4 AS
19
3087
by: Boris Borcic | last post by:
does x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) pick a random shuffle of x with uniform distribution ? Intuitively, assuming list.sort() does a minimal number of comparisons to achieve the sort, I'd say the answer is yes. But I don't feel quite confortable with the intuition... can anyone think of a more solid argumentation ?
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N? ----------------------------------------------------------------------- function Random(x) { return Math.floor(x*Math.random()) } gives a random integer in the range from 0 to x-1 inclusive; use « Random(N)+1 » for 1 to N where N>2. ...
0
9579
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...
1
9987
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
9857
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...
1
7404
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
6662
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
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
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
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.