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. 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
"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)
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
> 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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;
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |