Picking names at random from a list
Hi there, i have decide to learn Basic programming, and i have done the little things in AMOS.
the problem i have is i have 12 names that i want to be picked at random and then print to screen i.e name 5 name 1 name 12 name 10 and so on how do i do this please |
Quote:
Stores the 12 names into a string array, use a random generator to pick which one, mark that one with an integer array as being used, run the random generator until you get a name that hasn't been used. If AMOS doesn't have a random generator then there are several ways to make simple ones with floating pointer numbers. Somewhere I have a simple one but I can't find it, it does from 0 to 1.0 so you scale and offset it to get what you want. |
In AMOS you can use f.ex Rnd(10) to return an integer from 0 up to and including 10, and if you insert the line Randomize Timer in the beginning then you get different numbers every time you run your program.
|
What you need is a shuffling algorithm. Durstenfelds shuffling algorithm is the best one, as it can only pick each item in your list once, and it has no bias (meaning that any bias comes from your random number generator, and from not implementing some extra code to handle a certain kind of bias).
If you have questions, ask :great Fisher-Yates shuffle: http://en.wikipedia.org/wiki/Fisher–Yates_shuffle Durstenfeld shuffle for computer use: http://en.wikipedia.org/wiki/Fisher–...dern_algorithm |
Quote:
Doesn't a shuffle algorithm still have to have some kind of random generator in it? The shuffle still has to be random and therefor can still have a bias. |
Quote:
Another shuffling algorithm (a very bad one) picks two random numbers and swaps the entries in the list you're shuffling based on those two picks. This is a biased method. Even when your RNG is completely bias free, this shuffling algorithm's output will still be biased. |
It feels so weird going back to BASIC after so many years of using proper languages C, C++, etc., but I decided to give it a try. Booted up AMOS and came up with the following:
http://i.imgur.com/3FzDA.png (sorry. No idea how to export as text in AMOS, so a screenshot it is) It creates an array of 10 entries (well, 11 according to the manual, which is why there's index 10), selects a random number, then displays that entry. Super simple. A shuffle algorithm is overkill for such a simple task :) Hope that helps you, Champions_2002. |
Quote:
|
Quote:
|
Quote:
I just listed Durstenfelds algorithm because it's the most efficient. It allows you to shuffle any kind of array in-place, and it only has to re-pick a random number once in a while (although for big moduli this will take extra code to handle efficiently). Also, for smaller arrays, it's possible to use a modulus list, making the thing even faster. |
All times are GMT +2. The time now is 00:08. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.