srand

srand in C - with just one repetition

无人久伴 提交于 2019-12-24 17:14:53
问题 I'm trying to build a memory game and I want to ask how I can generate a randomic number with just one repetition. Like 1-1, 2-2, 3-3. I will paste here my function that I created and tell me if I have to create another function just to create a condition to create just a pair from numbers. // function to fulfill the table void preencher_mesa(int matriz[4][4], int dificuldade) { int i, j; int lim_col, lim_linha; // limits of the matriz for(i=0; i<4; i++) for(j=0; j<4; j++) matriz[i][j] = 0;

Usefulness of `rand()` - or who should call `srand()`?

六眼飞鱼酱① 提交于 2019-12-22 01:47:09
问题 Background: I use rand() , std::rand() , std::random_shuffle() and other functions in my code for scientific calculations. To be able to reproduce my results, I always explicitly specify the random seed, and set it via srand() . That worked fine until recently, when I figured out that libxml2 would also call srand() lazily on its first usage - which was after my early srand() call. I filled in a bug report to libxml2 about its srand() call, but I got the answer: Initialize libxml2 first then.

srand is too slow in C++ it return same number

旧城冷巷雨未停 提交于 2019-12-20 07:49:17
问题 I'm just trying to have simply RndInt(limit) function that will return random numbers with a limit as limit. cout << "Enter higher limit of random range:" ; cin >> limit; while(limit != 0) { // RndInt(limit); cout << "Random number smaller than " << limit << " is " << RndInt(limit) << endl; cin.get(); cin.get(); cout << "Other random number smaller than " << limit << " is " << RndInt(limit) << endl; cin.get(); cin.get(); cout << "There is " << RndInt(limit) << " ways I love you" <<endl ; cout

Problems with srand(), C++

风格不统一 提交于 2019-12-20 07:27:45
问题 I'm trying to write a program that generates a pseudorandom numbers using a seed. However, I'm running into problems. I get this error 39 C:\Dev-Cpp\srand_prg.cpp void value not ignored as it ought to be Using this code #include <iostream> #include <iomanip> #include <sstream> #include <limits> #include <stdio.h> using namespace std ; int main(){ int rand_int; string close ; close == "y" ; cout << endl << endl ; cout << "\t ___________________________________" << endl ; cout << "\t| |" <<

C++ 学习笔记

核能气质少年 提交于 2019-12-19 22:11:49
rand() 生成随机数,和一个范围的随机数 (有时候结果会不变) rand ( ) //生成一个随机数 rand ( ) % 100 //生成一个0~99的数 范围是0~小于给定的数 srand() 初始化随机数发生器,作为rand()生成随机数的种子。所以在使用的时候要先写一个srand()种下一颗随机数种子,然后在使用rand()这个长大的果树去生成一个随机数,可以说是从rand()这个树上随便摘一个果子。 但通常srand()又是有参数的。我理解这个参数是可以传递int 类型的,但是如果传递1,那么这个种子就是1不变了,那么最后得到的随机数也就可能会出现相同结果,一般的参数是time(0)或者time(NULL), time(0)表示当前时刻的秒数,那么当我们在不同时刻运行程序的时候就可以得到不同的随机数。 # include <ctime> //time(0)在这个头文件里 srand ( time ( 0 ) ) ; //随机数发生器 for ( int i = 0 ; i < 10 ; i ++ ) { cout << rand ( ) << endl ; //打印随机数 } string 使用string 进行输入输出的时候需要包含他的头文件 使用字符串需要先包含头文件 # include <string> string a = "张三" cout << a <<

What is the most correct way to generate random numbers in C with pthread

北城以北 提交于 2019-12-18 15:56:47
问题 I have several threads running concurrently and each of them must generate random numbers. I want to understand if there is a pattern to follow, to understand if it is correct to initialize the random generator with srand in the main thread or if every thread must initialize its own random generator. It seems that rand/srand have not been designed to be used with threads and I'm wondering how I can deal with threads and random numbers together. Thanks EDIT: I need pure random numbers, but I'm

Problems when calling srand(time(NULL)) inside rollDice function

若如初见. 提交于 2019-12-17 12:42:39
问题 When I used at first srand(time(NULL)) in rollDice() function it did not work. But when I put it in main, it works. Why is that? Can you tell me the logic? #include <stdio.h> #include <time.h> #include <stdlib.h> int rollDice(void) { return (1+rand()%6) + (1+rand()%6); } int main(void) { int roll; srand(time(NULL)); roll = rollDice(); printf("You rolled %d.\n", roll); enum Gamestatus {WON,LOST,CONTINUE}; enum Gamestatus status; while(status==CONTINUE){ printf("You are rolling again: \n");

srand function is returning same values

只愿长相守 提交于 2019-12-17 06:18:09
问题 Hey guys take a look at this program. /* The craps game, KN king page 218 */ #include <stdio.h> #include <time.h> #include <stdbool.h> #include <stdlib.h> int roll_dice(void); bool play_game(void); int roll_dice(void) { int roll; getchar(); srand((unsigned) time(NULL)); roll = rand() % 13; if(roll == 0) roll = roll + 1; return roll; } bool play_game() { int sum = 0, wins = 0, loss = 0, point; sum = roll_dice(); printf("You rolled: %d", sum); if(sum == 7 || sum == 11) { printf("\nYou won!\n");

Recommended way to initialize srand?

房东的猫 提交于 2019-12-16 19:59:21
问题 I need a 'good' way to initialize the pseudo-random number generator in C++. I've found an article that states: In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header ctime) is different each second, which is distinctive enough for most randoming needs. Unixtime isn't distinctive enough for my application. What's a better way to

Recommended way to initialize srand?

谁说我不能喝 提交于 2019-12-16 19:59:00
问题 I need a 'good' way to initialize the pseudo-random number generator in C++. I've found an article that states: In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header ctime) is different each second, which is distinctive enough for most randoming needs. Unixtime isn't distinctive enough for my application. What's a better way to