C:如何计算使用数组发生特定数字的次数

时间:2015-04-09 17:44:04

标签: c frequency

我编写了一个代码,用于跟踪随机移动的粒子的位置。这个想法是它意味着一次移动一个位置。它移动的方向是随机的。我遇到的问题是我可以跟踪“粒子”的次数。除了一组随机数之外,它一直处于某个位置。我知道这些随机数是错误的,因为输出的数字太大或者是负数。我是编程新手,知识有限。以下是我的代码。任何帮助将不胜感激。

#include<stdio.h>

int randomInt(int max)
 {
  return (random(2)%max);
 }

main()
 {
  int i;
  int position=1,r=1; /*Position indicates the position of the particle
                        and r represents where the particle will move
                        next*/ 
  int L=10,M=20;/*L is the number of sites and M is the number of hops*/
  int seed,n,sum=0;
  int frequency[position]; 

  //setup the random seed generator
  printf("\nEnter your value of seed\n");
  scanf("%d",&seed);
  srandom(seed);
  printf("\nThe value of the seed is %d\n",seed); 

  for(i=0;i<M;i++) //This loops around the total number of loops.
   {
    printf("\nThe particle is at position %d\n",position);
    n=randomInt(2);/*This chooses either the numbers 0 or 1 randomly */

    frequency[position]=frequency[position]+1;
    printf("This position has been visited %d times\n",frequency[position]);

    /*Below represents the conditions which will determine the movement
      of the particle*/

    if(n==0)
     {
       r=1;
     }
    if(n==1)
     {
       r=-1;
     }
    position = position + r;
    if(position==0)
     {
      position=L;
     }

    if(position==L+1)
     {
      position=1;
     }      

    }

} 

2 个答案:

答案 0 :(得分:1)

不太清楚,但这里有一些修复你的编程:

#include<stdio.h>                                                              
#include <string.h>                                                            


int randomInt(int max)                                                         
{                                                                              
    return (random() % max);                                                   
}                                                                              

main()                                                                         
{                                                                              
    int i;                                                                     
    int position=1,r=1; /*Position indicates the position of the particle      
                          and r represents where the particle will move        
                          next*/                                               
    int L=10,M=20;/*L is the number of sites and M is the number of hops*/     
    int seed,n,sum=0;                                                          
    int frequency[L];                                                          

    memset(frequency, 0, sizeof(frequency));                                   
    //setup the random seed generator                                          
    printf("\nEnter your value of seed\n");                                    
    scanf("%d",&seed);                                                         
    srandom(seed);                                                             
    printf("\nThe value of the seed is %d\n",seed);                            

    for(i=0;i<M;i++) //This loops around the total number of loops.            
    {                                                                          
        printf("\nThe particle is at position %d\n",position);                 
        n = randomInt(2);/*This chooses either the numbers 0 or 1 randomly */  

        frequency[position]=frequency[position]+1;                             
        printf("This position has been visited %d times\n",frequency[position]); 

        /*Below represents the conditions which will determine the movement    
          of the particle*/                                                    

        if(n == 0)                                                             
        {                                                                      
            r = 1;                                                             
        }                                                                      
        if(n == 1)                                                             
        {                                                                      
            r = -1;                                                            
        }                                                                      
        position = position + r;                                               
        if(position < 0)                                                       
        {                                                                      
            position = L - 1;                                                  
        }                                                                      

        if(position >= L)                                                      
        {                                                                      
            position = 0;                                                      
        }                                                                      

    }                                                                          

} 

答案 1 :(得分:0)

您的代码有两个问题:

  • 数组frequency的长度应为L
  • 应初始化数组frequency

一个建议:改变测试,以防你回来并希望增加或减少超过1。

这是由gcc main.c -o main

编译的代码
#include<stdio.h>
#include<stdlib.h>

int randomInt(int max)
{
    return (rand()%max);
}

int main(int  argc,char *argv[]){
    int i;
    int position=1,r=1; /*Position indicates the position of the particle
                        and r represents where the particle will move
                        next*/ 
    int L=10,M=20;/*L is the number of sites and M is the number of hops*/
    int seed,n,sum=0;
    int frequency[L]; 
    //initialization of frequency [can use memset()]
    for(i=0;i<L;i++){
        frequency[i]=0;
    }
    //setup the random seed generator
    printf("\nEnter your value of seed\n");
    scanf("%d",&seed);
    srand(seed);
    printf("\nThe value of the seed is %d\n",seed); 

    for(i=0;i<M;i++) //This loops around the total number of loops.
    {
        printf("\nThe particle is at position %d\n",position);
        n=randomInt(2);/*This chooses either the numbers 0 or 1 randomly */

        frequency[position]=frequency[position]+1;
        printf("This position has been visited %d times\n",frequency[position]);

        /*Below represents the conditions which will determine the movement
      of the particle*/

        if(n==0)
        {
            r=1;
        }
        if(n==1)
        {
            r=-1;
        }
        position = position + r;
        if(position<0)
        {
            position=position+L;
        }

        if(position>L-1)
        {
            position=position-L;
        }      

    }

    return 0;
} 

谨防使用rand()。根据{{​​1}}关于rand()

的文档
  

请注意,此模运算不会在跨度中生成均匀分布的随机数(因为在大多数情况下,此操作会使得较低的数字更有可能)。

特别是在您的情况下,如果v1 = rand() % 100;是偶数,则RAND_MAX将呈现向上循环的轻微趋势......