Jump to content



Photo
- - - - -

[casio C] Random Number


  • Please log in to reply
14 replies to this topic

#1 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 04 February 2007 - 10:05 AM

Hello, me again :)

my new problem is this:

i=(int)((float)rand()*52);

what I want is an integer between 0 and 51 based on a random calculation. "i" must range between 0 and 51 but it must be an integer because "i" will be a search index in an array.


EDIT:
i=(int)((float)rand()*52/(RAND_MAX+1));

i should read more into the tutorials for C and kucalcs pong code

:)

#2 kucalc

kucalc

    Casio Maniac

  • [Legends]
  • PipPipPipPipPipPipPipPip
  • 1422 posts
  • Gender:Male
  • Location:USA
  • Interests:Programming: C/C++, Fortran, LISP, COBOL 85 Standard, PHP, x86 and SH3 Assembly

    Computer graphics

  • Calculators:
    fx-9860G / fx-7400G Plus / Algebra FX 2.0+ / fx-9770G / CFX-9850G / CFX-9850GB+ / TI-89 / TI-nSpire

Posted 04 February 2007 - 02:32 PM

Yes, my Pong program uses the random number generator. :D

#3 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 04 February 2007 - 03:43 PM

ok i wonder if i have done it good, what i want to do is first fill a stock with the numbers 1 to 52 and then fill a stock with those numbers randomly. i used this code:

int new_stock;
int shuffle_stock;
int i;
int counter;

void shuffle()
{
	counter=0;
	for(i=0;i<=52;i++){
	new_stock[i]=i+1;
	}
	while(counter<52){
	i=(int)((float)rand()*52/(RAND_MAX+1));
		if(new_stock[i]!=0){
		shuffle_stock[counter]=new_stock[i];
		new_stock[i]=0;
		counter=counter+1;
		}
	}

}

But, it seems that the order does not change, i get the same order of cards (1 to 52 are cards) and i cannot see wher it is going wrong. Is the code i posted doing what i want it to do?

#4 kucalc

kucalc

    Casio Maniac

  • [Legends]
  • PipPipPipPipPipPipPipPip
  • 1422 posts
  • Gender:Male
  • Location:USA
  • Interests:Programming: C/C++, Fortran, LISP, COBOL 85 Standard, PHP, x86 and SH3 Assembly

    Computer graphics

  • Calculators:
    fx-9860G / fx-7400G Plus / Algebra FX 2.0+ / fx-9770G / CFX-9850G / CFX-9850GB+ / TI-89 / TI-nSpire

Posted 04 February 2007 - 04:18 PM

Use this:

int new_stock[52];
int shuffle_stock[52];
int i;
int counter;

void shuffle()
{
	counter=0;
	for(i=0;i<=52;i++){
		new_stock[i]=i+1;
	}
	while(counter<52){
		i=(int)((float)rand()*52/(RAND_MAX+1));
		if(new_stock[i]!=0){
			shuffle_stock[counter]=new_stock[i];
			new_stock[i]=0;
			counter=counter+1;
		}
	}
}


#5 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 05 February 2007 - 02:17 PM

ok, i know i ask alot :| so if i am annoying...well

but th ething is, i am rewriting my casio patience game in C, thought it would be a good exercise :D

i know the random number sequence i posted worked after all, so does the code kucalc showed me. But for some reason i get the same play table everytime i run the code i post below and i do not understand why. It looks like the play_table stock and the visible stock is filled in the same way everytime i run the program.

#include "fxlib.h"
#include "stdlib.h"
#include "stdio.h"

int heart[7][7]= {{0,1,1,0,1,1,0},{1,0,0,1,0,0,1},{1,0,0,1,0,0,1},{1,0,0,0,0,0,1},{0,1,0,0,0,1,0}
,{0,0,1,0,1,0,0},{0,0,0,1,0,0,0}};
int club[7][7]= {{0,0,0,1,0,0,0},{0,0,1,1,1,0,0},{0,1,0,1,0,1,0},{1,1,1,1,1,1,1},{0,1,0,1,0,1,0}
,{0,0,0,1,0,0,0},{0,0,1,1,1,0,0}};
int spade[7][7]= {{0,0,0,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,1,1,0},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1}
,{1,1,1,1,1,1,1},{0,1,1,0,1,1,0}};
int diamond[7][7]=
{{0,0,0,1,0,0,0},{0,0,1,0,1,0,0},{0,1,0,0,0,1,0},{1,0,0,0,0,0,1},{0,1,0,0,0,1,0}
,{0,0,1,0,1,0,0},{0,0,0,1,0,0,0}};
int x_symb;
int y_symb;
int x_card;
int y_card;
int x_sel;
int y_sel;
int i;
int j;
int k;
int l;
int play_table[7][20];
int visible[7][20];
int ace_table[4][13];
int draw_stock[24];
int pick_stock[24];
int moves;
int card_memory;
int counter=0;
unsigned int key;
int new_stock[52];
int shuffle_stock[52];

void shuffle()
{

		 	counter=0;
				for(i=0;i<=52;i++){
					new_stock[i]=i+1;
				}
				while(counter<52){
					i=(int)((float)rand()*52/(RAND_MAX+1));
					if(new_stock[i]!=0){
					shuffle_stock[counter]=new_stock[i];
					new_stock[i]=0;
					counter=counter+1;
					}
				}
}



void fill_playstocks()
{		
		/*fill table*/
			counter=0;
			for(i=0;i<=6;i++){
				for(j=i;j<=6;j++){
					play_table[j][i]=shuffle_stock[counter];
					shuffle_stock[counter]=0;
					counter=counter+1;
				}
			}
}

void visibility()
{
/*visibility*/
	for(i=0;i<=6;i++){
		for(j=i;j<=6;j++){
			if(play_table[j][i]!=0){
			visible[j][i]=1;
			}
			if(play_table[j][i+1]==0){
			visible[j][i]=2;
			}	
		}
		}
}

draw_table()
{
		/*draw Table*/
		for(i=0;i<=6;i++){
		for(j=i;j<=6;j++){
		x_card=(j*12);
		y_card=(i*2);
		Bdisp_PutDisp_DD();
		Bdisp_DrawLineVRAM(2+x_card,2+y_card,10+x_card,2+y_card);
		Bdisp_DrawLineVRAM(1+x_card,3+y_card,1+x_card,17+y_card);
		Bdisp_DrawLineVRAM(11+x_card,3+y_card,11+x_card,17+y_card);
			if(visible[j][i]==2&visible[j][i+1]==0){
			Bdisp_DrawLineVRAM(2+x_card,18+y_card,10+x_card,18+y_card);
			}

		}
		}
}

draw_symbol()
{
		/*draw symbols*/
		for(i=0;i<=6;i++){
		for(j=i;j<=6;j++){
			Bdisp_PutDisp_DD();
			if(visible[j][i]==2&visible[j][i+1]==0){
				x_symb=(j*12)+3;
				y_symb=(i*2)+4;
				if(play_table[j][i]<=13){
					for(k=0;k<=6;k++){
							for(l=0;l<=6;l++){
									if(heart[l][k]==1){
									Bdisp_SetPoint_DDVRAM(x_symb+k,y_symb+l,1);
									}
								}
							}
						} 	
					if(play_table[j][i]>=14&play_table[j][i]<=26){
					for(k=0;k<=6;k++){
							for(l=0;l<=6;l++){
									if(club[l][k]==1){
									Bdisp_SetPoint_DDVRAM(x_symb+k,y_symb+l,1);
									}
								}
							}
						} 	
				if(play_table[j][i]>=27&play_table[j][i]<=39){
					for(k=0;k<=6;k++){
							for(l=0;l<=6;l++){
									if(spade[l][k]==1){
									Bdisp_SetPoint_DDVRAM(x_symb+k,y_symb+l,1);
									}
								}
							}
						}
				if(play_table[j][i]>=40&play_table[j][i]<=52){
					for(k=0;k<=6;k++){
							for(l=0;l<=6;l++){
									if(diamond[l][k]==1){
									Bdisp_SetPoint_DDVRAM(x_symb+k,y_symb+l,1);
									}
								}
							}
						}
				}
				}
				}

		
}


int AddIn_main(int isAppli, unsigned short OptionNum)
{

Bdisp_AllClr_DDVRAM();
shuffle();
fill_playstocks();
visibility();
draw_table();
draw_symbol();


/*gameplaying section*/
while(1){
	   GetKey(&key);
				}
	return 1;
} 


/*leave unchanged*/
#pragma section _BR_Size
unsigned long BR_Size;
#pragma section
#pragma section _TOP
int InitializeSystem(int isAppli, unsigned short OptionNum)
{
	return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
}
#pragma section

i think it has something to do with de values every function returns but i am not shure. if anyone finds the time to help me... well thanks in advance

#6 Deimos

Deimos

    Casio Freak

  • Members
  • PipPipPipPip
  • 107 posts
  • Location:Poland
  • Interests:3d modelling, paper models, BASIC programming-AFX (just beginning)

  • Calculators:
    AFX 2.0+

Posted 05 February 2007 - 03:25 PM

The problem is that the rand() function is not a random number generator, but a pseudo-random number generator. It picks new numbers by adding a constant value, called the random seed to the previous ones. At a glance, they appear to be random, but in fact, they'll be exactly the same each time you run the program, just like you describe.
The solution of this problem is to initialize the seed with a different value on each program run. You can do it with srand() function.
The easiest and most commonly used way to get a random result is to pass the current time as the argument to the srand() function. In case you cannot read the time in your calc, you can create a main menu, in which some variable is being constantly incremented. When player chooses "new game" you just pass the current value of the timer as the argument to the srand() function. This should produce a random enough result too.

The other part you need - getting a random number from 0 to some maximal value can be done with:
int random= rand() % (max+1);
In your case, that'll be:
int i=rand()%52;

#7 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 05 February 2007 - 03:48 PM

:| how on earth... and yes you are right... i checked it three times and three times the shuffle stock was filled with the same numbers :| :|

i am stunned... and thank you :D i was going insane, everything worked but it just came with the same symbols each time i ran the program...

i will try your code, are you sure srand is in the casio C library?

sigh... never thought the random generator would be the problem

#8 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 05 February 2007 - 04:03 PM

sorry, me again

how should i use this srand?

like this:

srand(4);
i=rand()%52;

?

with 4 the value to be replaced with an integer wich will be incremented?

because this way the code does nothing at all :| i feel dumb :| i know the feeling, thats the only comfort in it :D

edit:
i should look first before asking
http://www.cprogramm.../fod/srand.html

:)

#9 Deimos

Deimos

    Casio Freak

  • Members
  • PipPipPipPip
  • 107 posts
  • Location:Poland
  • Interests:3d modelling, paper models, BASIC programming-AFX (just beginning)

  • Calculators:
    AFX 2.0+

Posted 06 February 2007 - 06:02 PM

The srand() function will work, if the rand() function is present. The problem might be caused by: (unsigned)time(NULL), as i have no idea if the calc you're programming even has a real time clock. If it works, that's great. if not, try to look for anything that will be different each time you run the program: it might be number of times you ran the program so far (if you have some way of storing it in an external file), amount currently free RAM (just like before - if you have a way of checking it), and if everything else fails, then try the incrementing variable trick. Just remember to increment the variable while waiting for user input, like:

<display a nice splash screen or something;) >
do
{
  i++;
}
while(<no button has been pressed>);
srand(i);

something like this:
<display a splash screen>
i++;
srand(i);
Won't work, since the timer will always be incremented only once, resulting in exactly the same seed (and the same "random" numbers) each time.

Using getch() to read user input won't do the trick eighter, since tgis function STOPS program execution while waiting for input, so during the wait, your seed variable won't be incremented. It would have to be a function that checks if a given button has been pressed, and if not, would continue program execution (again, i don't know the sdk you're using, so i don't know if such function is available :( ).

And yes, trying to get really random numbers CAN give quite a headache - i've been trough this much more than once :)

Anyway, let me know if you get it to work properly :)

#10 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 07 February 2007 - 04:59 AM

well, i got it working the way you described, i have a splash screen which waits for the user to hit exe. For now it works (testing purpouses). I am still learning C and the SDK at the same time so i might find a better way eventualy :). Thanks for taking time to solve the problem, i was thinking to rewrite thole hole program because i thought it had something to do with the array being not global enough (?) <- notice the panic ;)

I have so many new things to learn and stuff i really did not think it was the random generator causing the mistake (i am used that i am the one causing them ;))

The code needs many optimisations and shortcuts (notice i still use i=i+1 in stead of i++ for example) but first i want it to work in a way i understand :)

Thanks again for helping me, I was totaly freaking out when i found that everything was going as i wanted it to except the same cards kept showing up. Dumb thing is that the first search on internet on Random and C gave me many hits describing and solving my problem. Thanks for not answering like "is your google down?"

:)

#11 Deimos

Deimos

    Casio Freak

  • Members
  • PipPipPipPip
  • 107 posts
  • Location:Poland
  • Interests:3d modelling, paper models, BASIC programming-AFX (just beginning)

  • Calculators:
    AFX 2.0+

Posted 07 February 2007 - 05:41 PM

Always glad to help :)

#12 alias4399

alias4399

    Casio Freak

  • Members
  • PipPipPipPip
  • 180 posts
  • Location:Impossible to say.. Its not like this is a constant!

  • Calculators:
    CFX 9850GB Plus
    FX 9860G

Posted 08 February 2007 - 09:05 AM

Hi guys (Menno) :),

Whooo hooo the SDK has been released!! !@ :D

I've noticed a few things about CasioC:

=) You cannot declare a variable within a for statement
ie.
for(int i = 0;i < 5;i++)
// will not compile?

=) I've had trouble using pointers/references..
=) Ever tried printing on screen the contents of a variable?
eg.
Locate x,y;
Print((unsigned char*)levelNumber);
// does not compile?

If you guys could get any of these working or notice anything else, please let me know.

Apart from a few insignificant details, the SDK works like a charm.

=D

-Alias

#13 kucalc

kucalc

    Casio Maniac

  • [Legends]
  • PipPipPipPipPipPipPipPip
  • 1422 posts
  • Gender:Male
  • Location:USA
  • Interests:Programming: C/C++, Fortran, LISP, COBOL 85 Standard, PHP, x86 and SH3 Assembly

    Computer graphics

  • Calculators:
    fx-9860G / fx-7400G Plus / Algebra FX 2.0+ / fx-9770G / CFX-9850G / CFX-9850GB+ / TI-89 / TI-nSpire

Posted 08 February 2007 - 02:27 PM

Hello alias4399, I haven't seen you in a while. If you didn't know already, some games have been released already:

* Pong, Space Invaders and BoxWorld - me as in kucalc
* Aspirin and Sodoku - vanhoa
* Snake - Muelsaco

I've noticed a few things about CasioC:

=) You cannot declare a variable within a for statement
ie.

for(int i = 0;i < 5;i++)
// will not compile?


I think that happens because CASIO follows ANSI C (Standard C), you're not allowed to declare variables within statements.

=) I've had trouble using pointers/references..
=) Ever tried printing on screen the contents of a variable?
eg.

Locate x,y;
Print((unsigned char*)levelNumber);
// does not compile?


If you take a look at my Pong source code, you'll see how I print variables. Use sprintf() to copy a varible into a buffer, so it can be printed out

#14 alias4399

alias4399

    Casio Freak

  • Members
  • PipPipPipPip
  • 180 posts
  • Location:Impossible to say.. Its not like this is a constant!

  • Calculators:
    CFX 9850GB Plus
    FX 9860G

Posted 09 February 2007 - 02:54 PM

Hey kucalc, haven't see you in a while either.. (makes sense, really :D)

Thanks for the games notice@!

And thanks alot for your help, especially printing variables! I tried and got a 'TLB Error', not very user-friendly :(

;) Eagerly anticipating your next project,

-Alias

#15 kucalc

kucalc

    Casio Maniac

  • [Legends]
  • PipPipPipPipPipPipPipPip
  • 1422 posts
  • Gender:Male
  • Location:USA
  • Interests:Programming: C/C++, Fortran, LISP, COBOL 85 Standard, PHP, x86 and SH3 Assembly

    Computer graphics

  • Calculators:
    fx-9860G / fx-7400G Plus / Algebra FX 2.0+ / fx-9770G / CFX-9850G / CFX-9850GB+ / TI-89 / TI-nSpire

Posted 09 February 2007 - 08:56 PM

Hey, thanks! I'm sure you will like my next project (or should I say over 40 games!). :D You'll see when it comes out! It will probably solve many problems for those who don't like using the SDK.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users