[casio C] Random Number
#1
Posted 04 February 2007 - 10:05 AM
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
Posted 04 February 2007 - 02:32 PM
#3
Posted 04 February 2007 - 03:43 PM
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
Posted 04 February 2007 - 04:18 PM
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
Posted 05 February 2007 - 02:17 PM
but th ething is, i am rewriting my casio patience game in C, thought it would be a good exercise
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
Posted 05 February 2007 - 03:25 PM
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
Posted 05 February 2007 - 03:48 PM
i am stunned... and thank you 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
Posted 05 February 2007 - 04:03 PM
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
edit:
i should look first before asking
http://www.cprogramm.../fod/srand.html
#9
Posted 06 February 2007 - 06:02 PM
<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
Posted 07 February 2007 - 04:59 AM
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
Posted 07 February 2007 - 05:41 PM
#12
Posted 08 February 2007 - 09:05 AM
Whooo hooo the SDK has been released!! !@
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
Posted 08 February 2007 - 02:27 PM
* 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
Posted 09 February 2007 - 02:54 PM
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
Posted 09 February 2007 - 08:56 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users