Page 1 of 1

Script to Randomly choose text only one time??

PostPosted: Fri Jun 18, 2004 6:33 pm
by Just4Fun
Hi to All,

I'm taking a much needed break from database learning/programming. I am trying to finish a small demo for GE that I created a while back. As usual, I need some help... :?

I have a Switch (Select Case) statement with 4 Cases. Each Case holds a text string. The program randomly selects a string of text each time the player left clicks the mouse. The program allows the player only 4 mouse clicks.

My problem:
How do I stop all of the previously generated random strings of text from being generated again:?:

PostPosted: Fri Jun 18, 2004 8:05 pm
by jazz_e_bob
Good morning!

You need to be able to "tag" a string when it has been used.
Then only print an untagged random strings.

I've got to go to work this morning but will investigate further later today. :-)

PostPosted: Fri Jun 18, 2004 8:22 pm
by Just4Fun
Jazz-e-bob:
Work does get in the way! It is nice to be retired... ;-)
As always, I will be looking forward to your reply with thanks.

PostPosted: Sat Jun 19, 2004 11:30 pm
by jazz_e_bob
What we need it a non-recurring random number generator.

I am looking into it...

PostPosted: Sun Jun 20, 2004 9:11 am
by jazz_e_bob
Put this script in your global code:

------------------------------------- cut here

const MAX = 3;

int i;
int j;
int temp;
int number[MAX + 1];

void init_NR_RAND()
{
for ( i = 0; i < (MAX + 1); i++)
number[i] = i;
i = 0;
}

void shuffle_NR_RAND()
{
for (i = 1; i < (MAX + 1); i++)
{
temp = number[i];
j = rand(MAX + 1);
number[i] = number[j];
number[j] = temp;
}
i = 0;
}

int get_NR_RAND()
{
int result;
result = number[i];
i++;
if (i == (MAX + 1))
i = 0;
return result;
}

--------------------------------------------- cut here

This will give you the following routines/functions

init_NR_RAND();
shuffle_NR_RAND();
get_NR_RAND();

Once you have called init_NR_RAND and shuffle_NR_RAND you will be able to use get_NR_RAND() to generate non-recurring random numbers.

Set the value of MAX in the script if you need more than 4 values.

Now you can use the following style of code for your random text... (pseudo code)

switch(get_NR_RAND())
{
case 0:
text1
case 1:
text2
case 2:
text3
case 3:
text4
}

The random numbers WILL repeat (loop) if you keep calling get_NR_RAND.

You can re-shuffle the numbers at any time by calling shuffle_NR_RAND.

I hope this helps.

jazz.

PostPosted: Mon Jun 21, 2004 2:58 am
by Just4Fun
Jazz-e-bob,

Whew!
I think I will need to study this code in order to get a resonable understanding of what it is doing. It looks complicated, but oh so "Kool". :P Thanks for taking the time to figure this problem out. I can see that I would never have gotten it alone.

PostPosted: Mon Jun 21, 2004 4:57 am
by jazz_e_bob
Don't worry too much about the code. :)

It is all neatly hidden behind the routines/functions. 8)

(
All it does is create an array.
Fill the array with numbers and then shuffle the numbers.
The "get" function just reads each number one at at time.
)

You could also use the same function for an array of 52 cards!

PostPosted: Mon Jun 21, 2004 6:22 pm
by Just4Fun
Hi Jazz,
Where should I add the calls to init_NR_RAND and to shuffle_NR_RAND? I *really* want to get this code snip down because it sounds like it has lots of other neat applications. Card games for one... :)
However, something I'm doing isn't working. Would you look and see what you think my error is?

Here is code to show how I am doing the setup:

On MouseDown Event

init_NR_RAND();
shuffle_NR_RAND();

switch (get_NR_RAND())
{
case 0:
DestroyActor("MyActor");
CreateActor("MyActor....);
strcpy ("A Text String");
break;
case 1:
DestroyActor("MyActor");
CreateActor("MyActor....);
strcpy ("Another Text String");
break;
case 2:
DestroyActor("MyActor");
CreateActor("MyActor....);
strcpy ("Yada, Yada,Yada");
break;
case 3:
DestroyActor("MyActor");
CreateActor("MyActor....);
strcpy ("More Yada, Yada,Yada");
break;
{ //end switch

PostPosted: Mon Jun 21, 2004 6:45 pm
by jazz_e_bob
Move

init_NR_RAND();
shuffle_NR_RAND();

to the create actor event.


Cards are a good analogy for this system:

init - creates a sorted stack of cards
shuffle - shuffles the cards
get - reads the top card then places it at the back of the stack

:)

PostPosted: Mon Jun 21, 2004 7:16 pm
by jazz_e_bob
Looks like I need to patch my shuffling routine... :oops:


http://www.uidaho.edu/imc/2002/p02-21.html

Shuffling Bias Discovered in Basic Strategy Trainer

(Taken from http://www.blackjackinfo.com/news/bj022.htm)

This one's a real bummer for me. After a number of emails from visitors asking if the Blackjack Basic Strategy Trainer was dealing a random game of blackjack, I made a disappointing discovery. There was a flaw in the shuffling method that I used, which caused an unintentional bias in the game. The early hands being dealt in the shuffle were more likely to contain ten-valued cards than a truly randomly shuffled deck. Aces were actually less likely to appear, but the ten-card bias was enough to tilt the game in favor of the player.

The problem has now been corrected, but some of you may find an explanation of the problem interesting. (The rest of you can skip to the next story!)

Assume a single deck game, with 52 cards. My previous shuffle routine was as follows:
For each of the 52 spots in the deck, Swap that card with a randomly selected card (1 to 52).
That sounds reasonable, but it introduces a bias in the results. (Shuffling a 3-card deck (A,B,C) with this method leaves B as the first card 37% of the time, instead of the appropriate 33.3%.)

The new shuffling method is very similar, but no longer biased:
For each of the 52 spots in the deck, Swap that card with a card randomly selected between that spot and the end of the deck (n to 52).
Surprisingly, that subtle change of not allowing "backward swaps" makes the shuffle a random distribution.

It's frustrating to me that I've used an inaccurate shuffling routine over the years, and it's certainly an indication of how the smallest things matter when simulating a game like this. For those of you that have emailed me over the last few years asking about the integrity of the game on my site, I offer a sincere apology. The game was intended to be randomly dealt. Finally, it is.

Do the unbiased GE shuffle!

void shuffle_NR_RAND()
{
for (i = 1; i < (MAX + 1); i++)
{
temp = number[i];
j = i + rand(MAX + 1 - i);
number[i] = number[j];
number[j] = temp;
}
i = 0;
}

PostPosted: Tue Jun 22, 2004 4:39 pm
by Just4Fun
Hi Jazz,
Wouldn't you know, you hand me the solution and I'm still not getting things to work. I'm sorry to keep bothering you, but I think my problem is still in where I'm placing the code so I am walking through it once more. Maybe you can see my error?
I have one actor(MyActor). I placed my Switch(get_NR_RAND) case statements in a mouse down event --> script action in MyActor.

I then added a CreateActor in MyActor and placed the init_NR_RAND and the shuffle_NR_RAND there. When I run the program, it doesn't generate any random text to the screen... It is just blank.

If you are scratching your head after reading this, I would be happy to send you the demo file so that you could demonstrate the code placement properly. I know that is asking a lot of you, but as soon as that bit of code is done, the little demo is ready to upload to Makslane for the site so more people can look at how it works (at least in my application).

Thanks again for all you do for the members of this forum... Rachel

PostPosted: Tue Jun 22, 2004 10:34 pm
by jazz_e_bob
Best you send me the code.

jazz_e_bob@hotmail.com

:)