Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - Unique Random Numbers
Page 1 of 1

PostPosted: Sat Jan 12, 2002 2:00 am
by Gazza
I am having a problem trying to randomize an array.

Here's the program......

// Random Fill

string onez[6];
string twoz[6];

storez() {

int x,y,z;

onez[0]="Red";
onez[1]="Blue";
onez[2]="Green";
onez[3]="Orange";
onez[4]="Yellow";
onez[5]="Pink";

for (x=0; x<6;) {
z=random(5);
for (y=0; y<6;) {
if(twoz[y]!=onez[z]) {
twoz[x]=onez[z]; }
y++; }
x++; } }

display() {
int r;
while(r<6) {
puts(r+"."+twoz[r]+"\n");
r++;}}

main() {
storez();
display(); }

How can I fix this so that the array will not have duplicate entries.

Thanks Gareth

PostPosted: Sat Jan 12, 2002 11:09 am
by degv
There may be a more elegant solution but I would use a flag for each entry. Once you select onez[x] I'd set threez[x] to 1. I would only set twoz[x] to onez[z] if threez[x]!=1.

I hope this helps.

David

PostPosted: Sat Jan 12, 2002 8:50 pm
by Gazza
Thank's degv how would I go about this,

Basic used to use a command called Randomize Timer if there was a Randomize command in pocket c it would look like this :-

for (x=0; x<6;) {
z=randomize(5);
twoz[x]=onez[z];
x++;}

Maybe a good thing to add for Pocket C

This seems such an easy program to write, but can be quite complex in it own.<b></b><b></b>

PostPosted: Sun Jan 13, 2002 12:33 am
by jstadolnik
Do a search on the forum for the topic "Shuffling Cards". There is a fast algorithm there.

Joe

The PToolboxLib guy.
http://www.geocities.com/retro_01775/PToolboxLib.htm

PostPosted: Sun Jan 13, 2002 8:33 am
by Gazza
Thanks for you input,
These methods don't seem to work, or I am doing something wrong!!!

I think that it would be somethings like this

if(onez[z]!=twoz[y] && twoz[x]="")
{twoz[x]=onez[z]}

This does not work for some reason, but I imagine it would be something like this. <img src=icon_smile_sad.gif border=0 align=middle>

PostPosted: Sun Jan 13, 2002 3:59 pm
by jstadolnik
<pre id=code><font face=courier size=2 id=code>
shuffle(pointer array, int array_size) {
int x,tmp;
while(array_size) {
x=random(array_size--);
tmp=array[array_size];
array[array_size]=array[x];
array[x]=tmp;
}
}
</font id=code></pre id=code>

Joe


The PToolboxLib guy.
http://www.geocities.com/retro_01775/PToolboxLib.htm

PostPosted: Sun Jan 13, 2002 4:08 pm
by wolfgang
Joe,

you bastard, you stole my post while I was hacking away [:-)]

Here's the same idea in different wording:


randomize_array (int i_size, pointer p_array)
{
int i, i_rand;
string s_temp;

// loop over all elements
for (i = 0; i < i_size; i++)
{
// pick an arbitrary new position for the current element
i_rand = random (i_size);

// swap the current element with the one at the new position
// because of the magic auto-typecast this will also work for int, float
s_temp = p_array [i_rand];
p_array [i_rand] = p_array [i];
p_array [i] = s_temp;
}
}

print_array (int i_size, pointer p_array)
{
int i;

for (i = 0; i < i_size; i++)
{
puts (p_array [i] + "\n");
}
}

main ()
{
string s_my_array [6];

s_my_array [0] = "One";
s_my_array [1] = "Two";
s_my_array [2] = "Three";
s_my_array [3] = "Four";
s_my_array [4] = "Five";
s_my_array [5] = "Six";

randomize_array (6, s_my_array);

print_array (6, s_my_array);
}
<img src=icon_smile.gif border=0 align=middle><img src=icon_smile.gif border=0 align=middle><img src=icon_smile.gif border=0 align=middle><img src=icon_smile.gif border=0 align=middle><img src=icon_smile.gif border=0 align=middle>

PostPosted: Mon Jan 14, 2002 1:06 am
by Gazza
Well after many Coffee's and Little sleep I come up with this, which does the job.

// Random Fill

string one[6];
string two[6];

storez() {

int x,y,z,t;

one[0]="Red";
one[1]="Blue";
one[2]="Green";
one[3]="Orange";
one[4]="Yellow";
one[5]="Pink";

for(x=0; x<6;) {
z=random(6); t=0;
for(y=0; y<6;) {
if(two[y]==one[z]) {t=5; break;} y++;}
if(t==0) {two[x]=one[z]; x++;}
}}

display() {
int r;
while(r<6) {
puts(r+"."+two[r]+"\n");
r++;}}

main() {
storez();
display(); }

Thanks for all your input, I am looking at your program joe, what happens if the first random number is 2 and the second is 2, you can't depreciate the random counter cos this would not work, or have I missed something.

Gazza

PostPosted: Mon Jan 14, 2002 1:53 am
by jstadolnik
Your algorithm is rather inefficient compared to the others. I'd say the flaw in your logic, is that you are comparing the values of the array elements. When you shuffle a deck of cards do you look at the values as you do it?

In my alg, if the first random random number is 2, then the value in element two gets swapped with the top element. On the next pass, if a 2 occured again the value in element 2 gets swapped with the second to top value. Note that the "pool" of cards in play gets reduced by one with each loop pass and that once a card is swapped upwards, it exits play.

Wolfgang's algorithm is just as random but it is a bit slower because it uses a "for" loop rather than a "while", and it also has one more internal variable.

Joe


The PToolboxLib guy.
http://www.geocities.com/retro_01775/PToolboxLib.htm

PostPosted: Mon Jan 14, 2002 6:04 am
by Gazza
I can't seem to get your program to work Joe, I keep getting a global Error, I can see were your coming from, I have not studied pointers yet!!!, just starting out with pocket C moving from JAVA which does not use pointers.

Edited by - Gazza on 01/14/2002 00:06:22

Edited by - Gazza on 01/14/2002 00:07:18

PostPosted: Mon Jan 14, 2002 5:40 pm
by jstadolnik
<pre id=code><font face=courier size=2 id=code>
shuffle(pointer array, int array_size) {
int x;
string tmp; //needs to be a string
while(array_size) {
x=random(array_size--);
tmp=array[array_size];
array[array_size]=array[x];
array[x]=tmp;
}
}

main() {
string array[6]={"one","two","three","four","five","six"};
shuffle(array,6);
}
</font id=code></pre id=code>

Joe

The PToolboxLib guy.
http://www.geocities.com/retro_01775/PToolboxLib.htm

PostPosted: Mon Jan 14, 2002 9:07 pm
by Gazza
Awesome Joe, thanks Bud, I can see the difference in speed. thanks VM