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
5
votes
3 answers

random number generation without duplicates

I have to show some banners in a webpage.The number of banners will be with in 10 (max 10). I can set the number of banners and each banner folder in database. Banner images are stored in separate server folders based on category. Banners are…
Zendie
  • 1,176
  • 1
  • 13
  • 30
5
votes
3 answers

C++ srand, rand in a DLL strange behavior

I'm doing a host for an AI contest, and have a randomBot that choice random possible values. The bot has 2 exported functions: Init(_seed) and MakeMove() To play exactly same games host have a defined seed value for each bot. and its passes it in…
Spider
  • 927
  • 1
  • 9
  • 18
4
votes
4 answers

C custom random function

I would like to create a fast lightweight function in C language that returns a pseudo random unsigned char. The challenging part for me (an ANSI C programmer)is that I cannot use the or any other ready made functions. Any…
nonouco
  • 1,170
  • 1
  • 13
  • 25
4
votes
2 answers

Does the srand function work over multiple translation units?

If I call srand in my main function, would it also affect results in my functions in other translation units?
Serket
  • 3,785
  • 3
  • 14
  • 45
4
votes
3 answers

C++ random int function

Hello dear members of stackoverflow I've recently started learning C++, today I wrote a little game but my random function doesn't work properly. When I call my random function more than once it doesn't re-generate a number instead, it prints the…
Error1
  • 43
  • 1
  • 3
4
votes
4 answers

c++ While Loop termination with function

scratching my head on this as it was working just fine earlier but when I went to add some other functions suddenly my program freaked out and I can not get it back to what it was. class has me writing a rock/paper/scissors program to go up against…
4
votes
4 answers

Calling srand() twice in the same program

why is it when i call srand() at 2 very different points it cause numbers to not be random? Once i remove one of them it goes back to normal.
noobcoder
  • 6,089
  • 2
  • 18
  • 34
4
votes
4 answers

Is it possible to get an approximation to a seed based on a finite sequence of pseudo random numbers?

suppose I have some numbers that form a series for example: 652,328,1,254 and I want to get a seed that if I, for example, do srand(my_seed); I will get some kind of approximation with bounded error to my origonal sequence, when all numbers…
Alex
  • 768
  • 1
  • 5
  • 13
4
votes
4 answers

C++ random number generator

I am writing an lottory application. I have a function called generateLotteryNumbers this takes in an array and fills that array with 5 random numbers. What I want to do is have this function produce a different set of random numbers every time this…
KingJohnno
  • 602
  • 2
  • 12
  • 31
4
votes
5 answers

64 bits Seeds for random generators

I am currently running a multithreading simulation application with 8+ pipes (threads). These pipes run a very complex code that depends on a random sequence generated by a seed. The sequence is then boiled down to a single 0/1. I want this "random…
DarkZeros
  • 8,235
  • 1
  • 26
  • 36
4
votes
1 answer

srand() triggers Access not within mapped region

I am developing on Linux a program that operates on matrixs. I am getting problems in a piece of code that generates the random values of the matrix (it generates seg faults) Here the piece of code that triggers my problems. #include…
4
votes
4 answers

generate random RGB color using rand() function

I need a function which will generate three numbers so I can use them as RGB pattern for my SVG. While this is simple, I also need to make sure I'm not using the same color twice. How exactly do I do that? Generate one number at a time with simple…
Markus
  • 686
  • 1
  • 11
  • 18
4
votes
4 answers

Where to initialize random seed for use through multiple random modules?

So, every time I am developing something big, with multiple modules coming together to build a final functionality, I've been wondering the same question: Where to initialize the random seed if more than 1 module needs to use the random function? If…
penelope
  • 8,251
  • 8
  • 45
  • 87
3
votes
3 answers

generating random numbers - srand c++

I am having trouble using srand. i am trying to generate a random number in the interval 100 to 200. The number will keep being generated and placed in an array. Once the method is called again the same sequence of random numbers needs to be…
deepseapanda
  • 3,717
  • 8
  • 32
  • 39
3
votes
0 answers

Using srand with pthread generate the same number in C

I'm trying to run this very simple code which generate a random number between 0 to 99 using multithreading: #include #include #include #include void * NumGen(void * tid){ int a = 0; a = rand() %…
idop
  • 31
  • 1