Click here to Skip to main content
15,887,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone.

i have this problem. i was making a simple monopoly simulator and i needed a function that would generate random numbers from 1 to 6. i searched the net for the rand() func and yes it was working but the problem in my code that every time I run it, it seems to generate the same number over and over again though it still gives me numbers from 1 - 6.

C#
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int dice1(int iroll1);
int dice2(int iroll2);
main(){
int iDiceValue1, iDiceValue2,isumdice;
int iVal1=0, iVal2=0, again=0;
int ilocate=0;
do
{
iDiceValue1 = dice1(iVal1);
iDiceValue2 = dice2(iVal2);
isumdice = iDiceValue1 + iDiceValue2;

printf("\n%d \t ",isumdice);
ilocate = ilocate + isumdice;
if(ilocate>39){
ilocate = ilocate % 10;
}


if(ilocate==0)
{
printf("GO");

}
else if(ilocate==1)
{
printf("HILLSIDE AVENUE");

}

else if(ilocate==2)
{
printf("COMMUNITY CHEST");

}

else if(ilocate==3)
{
printf("HILLSIDE CRESCENT");

}

else if(ilocate==4)
{
printf("INCOME TAX");

}

else if(ilocate==5)
{
printf("TOWN HALL");

}

else if(ilocate==6)
{
printf("WELTON VALE");

}

else if(ilocate==7)
{
printf("CHANCE?");

}

else if(ilocate==8)
{
printf("BELLE VIEW");

}

else if(ilocate==9)
{
printf("VALLEY WALK");

}


else if(ilocate==10)
{
printf("JAIL");
}

else if(ilocate==11)
{
printf("ROCKY ROAD");

}
else if(ilocate==12)
{
printf("TESCO");

}
else if(ilocate==13)
{
printf("GULLOCK TYNING");

}
else if(ilocate==14)
{
printf("RACKVERNAL ROAD");

}

else if(ilocate==15)
{
printf("WESTFIELD CHAPEL");

}

else if(ilocate==16)
{
printf("WATERFORD PARK");

}
else if(ilocate==17)
{
printf("COMMUNITY CHEST");

}
else if(ilocate==18)
{
printf("ALDER TERRACE");

}
else if(ilocate==19)
{
printf("WESTFIELD TERRACE");

}
else if(ilocate==20){
printf("FREE PARKING");

}

else if(ilocate==21){
printf("SOUTH ROAD");

}

else if(ilocate==22){
printf("CHANCE");

}

else if(ilocate==23){
printf("SILVER STREET");

}

else if(ilocate==24){
printf("ST CHADS AVENUE");

}

else if(ilocate==25){
printf("SOMERVALE SCHOOL");

}

else if(ilocate==26){
printf("LONG BARNABY");

}

else if(ilocate==27){
printf("BLACKBERRY WAY");

}

else if(ilocate==28){
printf("SANSIBURYS");

}

else if(ilocate==29){
printf("CLAPTON ROAD");

}

else if(ilocate==30){
printf("GO TO JAIL");

}

else if(ilocate==31){
printf("BLUEBELL RISE");

}

else if(ilocate==32){
    printf("MONGER LANE");


}

else if(ilocate==33){
    printf("COMMUNITY CHEST");

}

else if(ilocate==34){
    printf("GREENFIELD WALK");


}

else if(ilocate==35){

printf("NORTON HILL SCHOOL");
}

else if(ilocate==36){

printf("NORTH ROAD");
}

else if(ilocate==37){

printf("CHANCE");
}

else if(ilocate==38){

printf("LUXURY TAX");
}

else{
printf("THE DYMBORO");

}
again++;
getch();

}
while(again!=100);
}

int dice1(int iroll1){

 int irollvalue1 = rand();
 irollvalue1= (irollvalue1 % 6) + 1;
return irollvalue1;
}

int dice2(int iroll2){

 int irollvalue2 = rand();
 irollvalue2= (irollvalue2 % 6) + 1;
 return irollvalue2;
}





could someone explain why it functions like that and what solutions/changes can you offer.

sorry for my bad coding skills. im still learning :) thank you for understang. Have a good day.
Posted

1 solution

You should seed the rand function once at the beginning of your code.

srand(time(NULL));
 
Share this answer
 
Comments
Reuben Cabrera 31-Jan-15 9:09am    
thank you. thats exactly the thing my prof told me a while ago :)
Reuben Cabrera 31-Jan-15 9:15am    
thank you. my codes works now :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900