Creating a randomizer that does not repeat picks

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Creating a randomizer that does not repeat picks

Post by Davidv » Sat Feb 13, 2021 10:23 pm

Welcome Hashim

I need some clarification of your question. The random function as described in the dictionary should provide a sufficiently random sequence and, as described under randomSeed, the starting point will be different each time you load the application.

It is possible to use the randomBytes property either to generate a new seed yourself or to construct the entire random sequence, though I think either would be overkill for a flags game, and randomBytes is much slower.

David

FiNN-6001
Posts: 8
Joined: Mon Feb 15, 2021 11:46 pm

Re: Creating a randomizer that does not repeat picks

Post by FiNN-6001 » Tue Feb 16, 2021 12:15 am

Hashim_553 wrote:
Sat Feb 13, 2021 9:46 pm
Hi I'm not only new to liveCode I'm also new to this website so I replied to this post.

I'm making a flags game and I'm trying to make the fags randomized and not repeated so I'm trying to make a randomizer that doesn't repeat itself how do I do that?
Hello! Welcome to LC and the LC forums! Here's how I would do it though I may be missing something.

Code: Select all

local flag -- Declares a new local variable 
put "US,UK,Ireland,Germany,France,Switserland,Brasil,Israel,Iran" into tFlags -- Puts a list of countrys into a temporary variable "tFlags", you can istaed put the names of the graphics etc.
split tFlags by comma -- Splits the variable "tFlags" into an array, --> tFlags [0] would be "US", tFlags[1] would be "UK" etc.

put the number of elements of tFlags into tLegnth -- Puts the legnth of the new array into temporary variable "tLegnth"
put random(tLegnth) into tKey -- Chooses a random key (because remember the keys of tFlags are numbers) of the array "tFlags"
put tFlags[tKey] into flag -- Puts the element of the random key into "flag"
delete variable tFlags[tKey] -- Removes the flag chosen from the array.

In hindsight tFlags should also be a local/global variable for this to work but I was to lazy to change it. Lines 1-2 should be called once (for example when the game opens) and the rest should be in a function/handler based on what would fit your project. Good luck and have fun

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9582
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Creating a randomizer that does not repeat picks

Post by dunbarx » Tue Feb 16, 2021 1:02 am

@ Hashim.

Perhaps a simpler test that you can play with. Make two fields. In fld 1 put several short lines of text. Lock the text of field 1. Put this in the field 1 script;

Code: Select all

on mouseup
   put random(the number of lines of me) into tLine
   put line tLine of me & return after fld 2
   delete line tLine of me
end mouseup
Click anywhere in field 1, and random lines of its text will build in field 2, and disappear from field 1.

Can you modify the script so that the line clicked on field 1 builds in fld 2, and is then deleted from field 1?

Craig

Post Reply

Return to “Talking LiveCode”