473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

random number picker code ??

hi there,

i'm after a small function that is kindof like the lottery picker programs,
i pass it some numbers, say (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) and i want
it to return me 3 random numbers, none being the same.

any help appreciated.
thanks,
Paul.
May 22 '06 #1
5 1637
> hi there,

i'm after a small function that is kindof like the lottery picker programs, i
pass it some numbers, say (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) and i want it
to return me 3 random numbers, none being the same.

any help appreciated.
thanks,
Paul.


Start with an array of booleans, index (+1) = lottery number.
Initialize with "false".

Get a random number (see Random class), see if that position is still
"false". If not, get a new (random) number, else set to "true".
Repeat 3 times.

Print all indexes (+1) with a "true" value.
Hans Kesting
May 22 '06 #2
"Milsnips" <mi******@hotmail.com>'s wild thoughts were
released on Thu, 18 May 2006 07:53:35 +0200 bearing the
following fruit:
hi there,

i'm after a small function that is kindof like the lottery picker programs,
i pass it some numbers, say (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) and i want
it to return me 3 random numbers, none being the same.

any help appreciated.
thanks,
Paul.


I think what you actually want is a shuffling algorithm,
then you just pick the first three numbers.


Jan Hyde (VB MVP)

--
People are like tea bags.
You have to put them in hot water before you know how strong they are. (Bill Rayburn)

May 22 '06 #3
int[] GetNums(int[] nums, int numToReturn)
{
int[] nm = new int[numToReturn];
if (nums.Length <= numToReturn)
{
for (int i=0;i<nums.Length;i++)
{
nm[i] = nums[i];
}
}
else
{
Random rnd = new Random(123);
for (int i=0;i<numToReturn;i++)
{
int idx = rnd.Next() % nums.Length;
nm[i] = nums[idx];
}
}

return nm;
}

Regards,
Tasos

May 22 '06 #4
> int[] GetNums(int[] nums, int numToReturn)
{
int[] nm = new int[numToReturn];
if (nums.Length <= numToReturn)
{
for (int i=0;i<nums.Length;i++)
{
nm[i] = nums[i];
}
}
else
{
Random rnd = new Random(123);
for (int i=0;i<numToReturn;i++)
{
int idx = rnd.Next() % nums.Length;
nm[i] = nums[idx];
}
}

return nm;
}

Regards,
Tasos


But how do you make sure a number is not chosen twice?
(what happens if "idx" is 6 for the second time? It will select
"nums[6]" for a second time)

Hans Kesting
May 22 '06 #5
He can iterrate through the table to check if the number is already
selected.

I'll post in a couple of minutes a revised sample :)

Tasos

May 22 '06 #6

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

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
4
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;
14
by: windandwaves | last post by:
Hi Folk I want a random list of numbers between 0 and 58 where each number can only occur once. I have written this ($f is an array): for($i = 0; $i < 13; $i++) { $rand =...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
3
by: td0g03 | last post by:
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...
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...

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.