Answered # random function generator

qaisu WaltP 2,905 ~s.o.s~ 2,560 John A 1,896 Infarction 503 ~s.o.s~ 2,560 vijayan121 1,152 Lardmeister 461 WaltP 2,905 Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

Hello muadh jamal!

you can simply generate a random number by the function rand() in the stdlib directory. However c++ can't generate true random numbers as they are pseudo-random means having some pattern so we need to seed them for this we use the function srand(), and we must give a seeding-number in the argument so we usually give it the time as argument, like this

srand(time(0));

now if you want to give a range you can do it by this syntax

(minimum+rand())%maximum;

for example if you want to generate the numbers between 1 and 10,000 simply do it this way

(1+rand())%10000;

so the complete code should be like this along with a function to do it :

--------------------------------

```
#include <iostream.h>
#include <stdlib.h>
int generate(int,int); //prototype for function
main(){
srand(time(0)); // Seed the random function
int min=1,max=10000; // variable to store max and min range
int number=generate(min,max);
cout << "The generated number is : "<<number;
}
int generate(int min,int max){
int num=(min+rand())%max;
return num;
}
```

-----------------------------------

You can alter the range by altering the min and max variables

Hope it helps

Regards,

QaiS

0

now if you want to give a range you can do it by this syntax

(minimum+rand())%maximum;

for example if you want to generate the numbers between 1 and 10,000 simply do it this way(1+rand())%10000;

Not quite. it's `minimum + (rand() % maximum);`

0

Not quite. it's

`minimum + (rand() % maximum);`

Not quite, its: `minimum + (rand() % (maximum - minimum));`

This will produce random numbers in the range minimum inclusive and maximum exclusive.

;)

0

Not quite, its:

`minimum + (rand() % (maximum - minimum));`

This will produce random numbers in the range minimum inclusive and maximum exclusive.

Not quite, it's

`minimum + rand() / (RAND_MAX / (maximum - minimum) + 1 );`

To ensure proper distribution.

:twisted:

(sorry, I couldn't help it :mrgreen:)

0

Not quite, it's

`minimum + rand() / (RAND_MAX / (maximum - minimum) + 1 );`

To ensure proper distribution.

:twisted:

(sorry, I couldn't help it :mrgreen:)

Actually, according to this very enlightening read, you're all wrong...

(sorry, I couldn't help it either) :mrgreen:

0

Oops.... so it is. Thanks

Sannie

I guess its payback time... :mrgreen:

Not quite, it's

`minimum + rand() / (RAND_MAX / (maximum - minimum) + 1 );`

To ensure proper distribution.

(sorry, I couldn't help it )Actually, according to this very enlightening read, you're all wrong...

(sorry, I couldn't help it either)

I never said my code ensured proper distribution. Atleast it was logically correct. :D

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

0

this is what the linux man pages say (in part) about rand(3):

" ... The versions of **rand()** and **srand()** in the Linux C Library

use the same random number generator as **random()** and **sran****dom()**, so the lower-order bits should be as random as the

higher-order bits. However, on older **rand()** implementa

tions, the lower-order bits are much less random than the

higher-order bits.

In *Numerical* *Recipes* *in* *C:* *The* *Art* *of* *Scientific* *Computing*

(William H. Press, Brian P. Flannery, Saul A. Teukolsky,

William T. Vetterling; New York: Cambridge University

Press, 1992 (2nd ed., p. 277)), the following comments are

made:** "If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in**

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

Random-number generation is a complex topic. The *Numeri**cal* *Recipes* *in* *C* book (see reference above) provides an

excellent discussion of practical random-number generation

issues in Chapter 7 (Random Numbers). ..."

0

Now we have all seen that our moderators are less than perfect:

```
// typical example for seeded random integers in a given range
#include <stdlib.h>
#include <stdio.h>
#include <time.h> // time()
int main(void)
{
int i, min, max, rn;
srand(time(NULL)); // random seed on most compilers
min = 1;
max = 10000;
printf("Ten random numbers from 1 to 10000\n\n");
for(i = 0; i < 10; i++)
{
rn = min + (int)((float)max*rand()/(RAND_MAX+(float)min));
printf("%d\n", rn);
}
return 0;
}
```

0

Now we have all seen that our moderators are less than perfect

And so are most that try one-upmanship ;)

No formatting

Poor commenting

Casts to float that are useless.

:rolleyes:

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...