随机数函数 [英] random number function

查看:134
本文介绍了随机数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有这个问题。我正在制作一个简单的垄断模拟器,我需要一个从1到6生成随机数的函数。我在网上搜索了rand()函数,是的,它正在工作但我的代码中的问题是每次运行它时,它似乎一遍又一遍地生成相同的数字虽然它仍然给我1到6的数字。



 #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 ,再次= 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( 收入税);

}

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

}

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

}

else if (ilocate = = 7
{
printf( 机会?);

}

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( 社区CHEST);

}
else if (ilocate == 18
{
printf( ALDER平台);

}
else if (ilocate == 19
{
printf( WESTFIELD平台);

}
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学校);

}

else if (ilocate = = 26 ){
printf( LONG巴纳);

}

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致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);

}
再次++;
getch();

}
while (再次!= 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;
}











可以解释为什么它的功能如此以及您可以提供哪些解决方案/更改。



对不起我糟糕的编码技巧。我还在学习:)谢谢你的帮助。祝你有个美好的一天。

解决方案

你应该在你的代码开头一次播种rand函数。



 srand(time(NULL)); 


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.

#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.

解决方案

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

srand(time(NULL));


这篇关于随机数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆