How to Generate Different Random Numbers in a Loop in C++

How do I generate different random numbers in a loop in C?

Just put this in the for loop:

r = (rand()%6)+1;

And declare r outside of it like this:

int r;

OR you don't use r and the program would look something like this

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

int main() {

int count, diceRoll;
srand(time(NULL));

printf("\t\t\t Welcome to Dice Game!\n");

for(count = 0; count < 6; count ++){
diceRoll = (rand()%6)+1;
printf(" %d \n", diceRoll);
}

return 0;

}

How to generate different random numbers in a loop in C++?

Don't use srand inside the loop, use it only once, e.g. at the start of main(). And srand() is exactly how you reset this.

random number in while loop [C programming]

You only need to seed the random number generator once.

So run this line outside of your random function:

srand(time(NULL));

.. in your main function instead:

int random(int range){
int num;
num = rand() % range;
return num;
}

int main(){
int cnt = 0;
int i;
int j;

// Seed random number generator
srand(time(NULL));

while (cnt <= 20){
i = random(5);
j = random(10);
printf("%d\n",i);
printf("%d\n",j);
printf("\n");
cnt += 1;
}
return 0;
}

Random number in FOR loop expression in C language

i=0 will be evaluated exactly once, before the loop begins.

i < rand() will be executed before each loop iteration - if the condition is ever false (IOW, if i >= rand()) then the loop will exit. For each iteration of the loop i is compared to a different random number1. If that's not what you want, then you need to save the result of rand() to a different variable and compare against that:

for ( int val = rand(), i = 0; i < val; ++i )
// loop body

This will call rand() exactly once and save the result to val, and then i is compared to val each time through the loop.

++i is executed after each loop iteration.



  1. Remember that rand() is a pseudo-random number generator - it uses a deterministic algorithm to generate a sequence of numbers, and the sequence it generates will be the same each time you run your program unless you use srand to seed it with a different starting value (typically by using the value returned from time).

Generate two different random numbers in C

srand(time(0));  

This has to be outside loop.

In loop you reseed the RNG , whereas you should be doing it once.

How to generate a random int in C?

Note: Don't use rand() for security. If you need a cryptographically secure number, see this answer instead.

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

srand(time(NULL)); // Initialization, should only be called once.
int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.

On Linux, you might prefer to use random and srandom.

C: multiple child process each generate different random number

You only call rand() once in each child (outside the while (count < 3) loop, and then you print out that same number three times in a loop. Note that you do not assign any new value to r between loop iterations, so naturally its value hasn't changed when you print it again.

If you want three different numbers, call rand() three times (inside the loop). Replace

        int r = rand() % 30;
int count = 0;
while (count < 3){
printf("Child %d: %d\n",i+1,r);
count++;
}

with

        int count = 0;
while (count < 3){
int r = rand() % 30;
printf("Child %d: %d\n",i+1,r);
count++;
}

random numbers in c inside a loop

If you reboot your pc you'll see that the results will be different.

Random is not clever as you expect, and, to tell you the truth, is not properly random! (:

By the way, to provide a different seed to the random alghoritm, you have to call, only one time:

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

srand(time(0));

Take a look to the documentation!

edit

To improve your code:

int randN = 100;
switch (l) {
case 1:
break;
case 2:
randN = 500;
break;
case 3:
randN= 1000;
break;
default:
randN = rand() % 1000 + 1;
break;
}
n = rand() % randN + 1;
printf("I picked a number between 1 and %i.\n", randN);


Related Topics



Leave a reply



Submit