English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 03 January 2012, 15:38   #1
Champions_2002
Junior Member
 
Champions_2002's Avatar
 
Join Date: May 2002
Location: Manchester
Age: 54
Posts: 61
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
Champions_2002 is offline  
Old 03 January 2012, 19:07   #2
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 71
Posts: 351
Quote:
Originally Posted by Champions_2002 View Post
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

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.
Ed Cruse is offline  
Old 03 January 2012, 19:59   #3
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
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.
Leffmann is offline  
Old 03 January 2012, 20:16   #4
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,751
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

Fisher-Yates shuffle: http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Durstenfeld shuffle for computer use: http://en.wikipedia.org/wiki/Fisher–...dern_algorithm
Thorham is offline  
Old 04 January 2012, 17:29   #5
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 71
Posts: 351
Quote:
Originally Posted by Thorham View Post
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

Fisher-Yates shuffle: http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
Durstenfeld shuffle for computer use: http://en.wikipedia.org/wiki/Fisher–...dern_algorithm

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.
Ed Cruse is offline  
Old 04 January 2012, 18:14   #6
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,751
Quote:
Originally Posted by Ed Cruse View Post
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.
The only source of bias in the Durstenfeld shuffling algorithm comes from not handling mod bias (I can give you an explanation of this). When this is taken care of, the algorithm itself is bias free, and the only source of bias comes from the RNG used, and is therefore external (because the RNG isn't part of the shuffling algorithm).

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.
Thorham is offline  
Old 04 January 2012, 22:43   #7
Radertified
Registered User
 
Join Date: Jan 2011
Location: -
Posts: 728
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:



(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.
Radertified is offline  
Old 05 January 2012, 05:33   #8
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,751
Quote:
Originally Posted by Radertified View Post
A shuffle algorithm is overkill for such a simple task
Yes, it is. I mis-read the question. But it may still be useful if items shouldn't be picked more than once, even though it probably doesn't apply here.
Thorham is offline  
Old 05 January 2012, 21:05   #9
Ed Cruse
Registered User
 
Join Date: Sep 2007
Location: Las Cruces, USA
Age: 71
Posts: 351
Quote:
Originally Posted by Thorham View Post
The only source of bias in the Durstenfeld shuffling algorithm comes from not handling mod bias (I can give you an explanation of this). When this is taken care of, the algorithm itself is bias free, and the only source of bias comes from the RNG used, and is therefore external (because the RNG isn't part of the shuffling algorithm).

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.
Then wouldn't the method I described be as bias free as the RNG. It's real simple, do an RNG from 0-11 and pick from the list and then mark that entry as picked. Then run the generator again and use that pick unless it's already been picked in which case run the generator again until you get an unpicked entry. Now obviously when you get to the end of the list the generator will get run a lot because the probability of an entry having already been pick is high. There should be no bias though.
Ed Cruse is offline  
Old 05 January 2012, 23:22   #10
Thorham
Computer Nerd
 
Thorham's Avatar
 
Join Date: Sep 2007
Location: Rotterdam/Netherlands
Age: 47
Posts: 3,751
Quote:
Originally Posted by Ed Cruse View Post
Then wouldn't the method I described be as bias free as the RNG. It's real simple, do an RNG from 0-11 and pick from the list and then mark that entry as picked. Then run the generator again and use that pick unless it's already been picked in which case run the generator again until you get an unpicked entry. Now obviously when you get to the end of the list the generator will get run a lot because the probability of an entry having already been pick is high. There should be no bias though.
I really don't know. It has to be tested But re-picking numbers doesn't seem to be a problem (although these things can be tricky to judge properly).

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.
Thorham is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random question 251Mario project.EAB 1 16 May 2013 02:19
Picking up the PCMCIA compact flash adapter in WB3.1 pubzombie New to Emulation or Amiga scene 10 18 March 2011 15:07
Executioner - picking up stuff Crowley support.Games 0 08 June 2010 04:12
List in alphabetical order in dropdown list of Floppy Drives Zed request.UAE Wishlist 1 06 February 2010 17:26
Amiga Demoscene Archive: picking up speed... Paul News 0 21 February 2005 11:40

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 00:46.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.11432 seconds with 15 queries