Questions tagged [srand]

a function which initializes the pseudo-random number generator in C/C++ or PHP

A random seed is used to determine the initial state of a pseudo-random number generator. Such generators are used in many programming languages.

For a specific generator and a single value of seed the sequence of generated numbers will be the same.

Sample code:

#include <cstdlib>

int seed = 111;
int arr1[10];
int arr2[10];
srand (seed);
for (int a=0; a<10; a++) {
  arr1[a] = rand();
}
srand (seed);
for (int a=0; a<10; a++) {
  arr2[a] = rand();
}
for (int a=0; a<10; a++) {
  assert arr1[a] == arr2[a];
}
396 questions
-2
votes
3 answers

rock paper scissors game in c. Can someone tell where my fault is?

The computer always gives the same input. For example :example or example2 As in the example, I always choose the rock and the computer always chooses the paper. What should I do to make the computer give a different result? Probably there is…
-2
votes
2 answers

Why does this code always generate zero when I want to generate random numbers? How can I fix the problem?

#include #include #include #include using namespace std; class mRND { public: void seed() { srand(time(0)); _seed = rand(); } protected: …
raneem
  • 1
  • 3
-2
votes
1 answer

rand() doesn't give me random numbers

I want to print 1000 random numbers saved in a array. Numbers have to be between 1 and 10000. I put srand(time(NULL)) in my main function and the array have to be filled with random numbers in my init function. The ausgabe function is for formatted…
-2
votes
1 answer

Write a program in C that uses the rand() function to create 1000 structs

I have these challenges: Define a struct containing 4 data types Write a program in C that uses the rand() function to create 1000 instances of these structs Inserts them in the linked list and prints out the first 10 to the console. Modify the…
Toomey86
  • 1
  • 1
-2
votes
2 answers

srand not working properly

I'm writing a program that needs to fill two vectors with random ints(less than 1000) using 2 different seeded lists. when I try to use srand with the seeds I am supposed to use it gives me a very strange output. here is the code I've written so…
-2
votes
1 answer

C++ Simple Dice roll - how to return multiple different random numbers

I am pretty new to C++ and am trying to make a simple die roll with a Die class/main. I can get a random number within my range 1-dieSize, however, each time I "roll the dice" it just gives me the same random number. For example, when I roll this…
Ali
  • 9
  • 1
  • 2
  • 3
-2
votes
2 answers

Create a dynamic array and assign random integer values to it?

int* create_array(char category, int n){ int *a; a = malloc(n* sizeof(int)); for (int i = 0; i < n; i++) { int x; srand( time(NULL)); x = rand(); a[i] = x; } When I print this code, it just prints the same random variable…
naikrima
  • 91
  • 1
  • 1
  • 6
-2
votes
2 answers

Why is srand() not being stored in my variable?

This beginner's program I am trying to make is basically a program where a truly random number is generated and the "player" has to guess the number. The computer will respond "too high" or "too low" until the player guesses the right number. I got…
Matthew
  • 21
  • 4
-2
votes
3 answers

Loop iterating only through half of the elements

Here is my code. My question is, why does it keep printing only 5 numbers? Why won't it print 10 like it's supposed to? #include #include #include int main(){ int r, col; srand((unsigned)time(NULL)); …
CrushClaw
  • 1
  • 2
-2
votes
3 answers

Generate a random number doesnt work when calling function twice

I am trying to get 2 different types of numbers/colors when I am calling my function twice, but it does not work. I've seeded the numbers/function but its still not working. Here's how my code looks: #define _CRT_SECURE_NO_WARNINGS #include…
Philly
  • 13
  • 1
  • 4
-2
votes
2 answers

Why am i getting out-of-range error?

I can't seem to find where my issue is. Its a three file program with aDie class in one file, aHistogram class in another file, and the main.cpp file. It is supposed to print a histogram constructed with X's to show how many times the die landed on…
-2
votes
1 answer

How to use an if statement in a string array?

The following code is supposed to randomly pick a string from the array and then say "Yay!" if Happy has been chosen. #include #include #include #include using namespace std; int main() { …
Yetoo
  • 47
  • 1
  • 9
-2
votes
1 answer

c strange use of srand and rand c

Can you help me to understand what is the meaning of these randomization? I have found it in a c code that I have to translate, it return always 41: int main(){ srand(1); printf("\n%d",rand()); } How I can emulate the srand(1) and rand() in…
michele
  • 26,348
  • 30
  • 111
  • 168
-2
votes
1 answer

Dice game program will not run but will not stop at 5 rolls

I am getting two errors: Error 3 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup and: Error 4 error LNK1120: 1 unresolved externals Since I can not get the program to run, I am also unsure if Case…
-2
votes
2 answers

Random Number generator to output numbers in ascending order

To start with, l'm an early beginner programmer and would like some help with this please. I have written the following code, which from what l have tested generates: 5 random numbers between: 1 and 39 //num1gen to num5gen - e.g.group A 1 random…
Mike Ericson
  • 23
  • 1
  • 2
  • 7