我如何获得随机数? [英] How do I get a random number?

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

问题描述

你好,每个人



我一直在尝试使用C语言获得1到9 999 999之间的随机数

并且看起来并不像工作



我尝试过:



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;
}







这会返回大值,而价值需要介于1到9999999之间



有人可以帮忙吗?谢谢!




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

could anyone help ? thanks !

推荐答案

引用:

这会返回大值,而vallues需要范围介于1和9999999之间

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



否,此代码不返回任何内容,因为它无法编译。显示真实代码。

看起来您还需要学习 rand()功能。



< French>


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

<French>

rand()*RAND_MAX/10 - rand()%10000



L'utilization de plusieurs rand()ne rendra pas le resultatplusaléatoire,c'est inutile。

nombreMystere n'estpasdeclaré dans ton code,çacuquequeune erreur de compilation。

< /; French>


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>


这在Google中很容易搜索: C ++ 0到100之间的随机数 [ ^ ]

This is easily searchable in Google: "C++ random number between 0 and 100[^]"
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.


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

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