2

I've been trying to write a small C function for generating random values. The problem is that it returns same value each time the function is called in for loop. I understand the problem is that srand is seeded with NULL. What I want to know is how to correct it, so that on each iteration of for loop the function returns a different value. Here's the code:

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

int randInt(int,int);

void main(){

    int min=100, max=200,i=0;

    for(i;i<11;i++){ printf("%d \n",randInt(min,max));  }

}


int randInt(int a,int b){

    srand(time(NULL));
    int randValue;
    randValue=1+(int)rand()%(b-a+1);
    return randValue;

}

Please let me know if you have a solution or can post some reference to a solution. Thank you in advance !

Edit : Encountered Problem #2, after having replaced srand(time(NULL)) into main, every iteration now generates numbers bellow my range, i.e. originally i wanted numbers between 100 and 200, but it also included numbers between 0 and 100. This was solved with randValue=a+(int)rand()%(b-a+1); as suggested in the comments

7
  • 5
    Don't call srand inside the random function. Commented Sep 13, 2014 at 10:41
  • To expand on Retired Ninja's comment. srand is used for (one time)initialization, then rand is used to retrieve the value. Commented Sep 13, 2014 at 10:46
  • Thank you, Ninja, it worked ! Now, just one more small question - I'm trying to generate numbers from 100 to 200, however the function generated values bellow 100. How do i fix that? Commented Sep 13, 2014 at 10:47
  • 1
    Bung srand(time(NULL)); just after main Commented Sep 13, 2014 at 10:51
  • 1
    Note: Typically the srand(time(NULL)); in main() trick does not work to "make random value function return different value each time" if the code is executed twice in the same second. An alternative is to pull the seed in srand(seed) value from a file and increment it each program run. Many approaches exist to solve this problem. Commented Sep 13, 2014 at 12:59

3 Answers 3

2

Put srand(time(NULL)); in main just after the {

To generate random numbers in the range 100-200,instead of adding 1,add 100 or a to

randValue=(int)rand()%(b-a+1);

So that it looks like:

randValue=(int)rand()%(b-a+1)+100;
Sign up to request clarification or add additional context in comments.

7 Comments

this will not solve his second problem
What is his Second Problem? There is nothing more in the question
you should really read comments under the question
He should have mentioned that in his question .....
@Xieerqi , You can still edit your question to add that too!
|
1

This will print random value each time between 100-200:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int randInt(int a,int b)
{
    int randValue;
    randValue = (rand() % (b-a+1)) + a;
    return randValue;
}

int main(void) {
    int r=0, i = 0;

    srand(time(NULL));
    do 
    {
        r = randInt(100,200);
        printf("%d\n",r);
        i++;
    }while(i < 11);
    puts("Done!");

    return 0;
}

1 Comment

removed my previous comment +1
1

I am using min and max insead of a and b. It is better for future readers of your code

  1. You should use srand(time(NULL)) only once at initialization e.g in your main()
  2. You probably wanted min + rand() % (max - min + 1) instead of rand()%(b-a+1)

2 Comments

-1 You cannot Use min or max in the randInt function as they are local variables
@CoolGuy ups read carefully i did mention that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.