Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello everyone

ive been trying to get a random number between 1 and 9 999 999
using C language and it doesnt seem to work

What I have tried:

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

int main(void){
	int i = 0;
	int nombre_aleatoire = 0;
	srand(time(NULL)); 
	for(i=0; i<1; i++){
		nombre_aleatoire = rand()*RAND_MAX/10 - rand()%10000;  
		printf("%d ",nombreMystere);
		
	}
	return 0;
}



this returns big values while the vallues need to range between 1 and 9999999

could anyone help ? thanks !
Posted
Updated 26-Mar-17 6:20am

This is easily searchable in Google: "C++ random number between 0 and 100[^]"
C
num = (int)(rand() * 9999999) + 1;

Using the "%" operator to limit the range has the side effect of generating lower values slightly more often than higher ones.
 
Share this answer
 
Comments
Member 13079820 26-Mar-17 12:49pm    
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){
int i = 0;
int nombre_aleatoire = 0;
srand(time(NULL));
for(i=0; i<88; i++){
nombre_aleatoire = (int)(rand() * 9999999) + 1;
printf("%d ",nombre_aleatoire);

}
return 0;
}


i tried to change the i value (to see more random values) and it generates negative numbers. why?
Dave Kreskowiak 26-Mar-17 14:23pm    
It's been a LONG time since I've done random in C/C++. Things have changed a bit...

Rand() returns an integer from 0 to 32767, not from 0 to 1. Multiply 32767 by 999999 and you get a number that overflows an int, thus giving the strange results.

The new code has a couple of methods of returning a range of integers, one biased and one not:

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

// Define two methods of generating random numbers;
int GetRandomModulo(int min, int max);
int GetRandomModern(int min, int max);

int main()
{
	int i = 0;
	int num = 0;
	srand(time(NULL));

	for (i = 0; i < 88; i++)
	{
		// use

		num = GetRandomModulo(1, 9999999);

		// or 

		num = GetRandomModern(1, 9999999);

		printf("%d\r\n", num);
	}

    return 0;
}

int GetRandomModulo(int min, int max)
{
	int range = max - min + 1;
	int num = rand() % range + min;

	return num;
}

int GetRandomModern(int min, int max)
{
	// This code is should really be wrapped in its own class to
	// prevent constantly reseeding the RNG engine every time it's
	// called.

	// Seed a Mersenne Twister RNG engine
	static std::random_device seed;
	static std::mt19937 engine(seed());
	static std::uniform_int_distribution<int> ditribution(min, max);

	return ditribution(engine);
}
Quote:
this returns big values while the vallues need to range between 1 and 9999999

No, this code return nothing because it does not compile. Show real code.
Looks like you need to study the rand() function too.

<French>
C++
rand()*RAND_MAX/10 - rand()%10000

L'utilisation de plusieurs rand() ne rendra pas le resultat plus aléatoire, c'est inutile.
nombreMystere n'es tpas declaré dans ton code, ça provoque une erreur de compilation.
</;French>
 
Share this answer
 
Comments
Member 13079820 26-Mar-17 12:53pm    
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){
int i = 0;
int nombre_aleatoire = 0;
srand(time(NULL));
for(i=0; i<88; i++){
nombre_aleatoire = rand()*RAND_MAX/10 - rand()%10000;
printf("%d ",nombre_aleatoire);

}
return 0;
} * i tried something and forgot to delete it.

we stuudied (rand()%1000 for example) but not with big values
and it doesnt work with vaalues larger than 32747 (rand_max)
Patrice T 26-Mar-17 13:18pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

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