How am I using rand() in C wrong?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 6151 Views - Last Post: 16 August 2009 - 08:22 AM Rate Topic: -----

#1 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

How am I using rand() in C wrong?

Posted 13 August 2009 - 02:32 PM

I'm trying to make a function than will generate a random number. So I made this:
random(int number){
	number = rand() % 1 + 9;
}



Then, later in the program, I use this:
firsts[10] = random(ran[4]);



But whenever I run the program, firsts[10] always equals 9.
Please help.

This post has been edited by jeff544: 13 August 2009 - 02:35 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How am I using rand() in C wrong?

#2 Guest_Neumann*


Reputation:

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 02:36 PM

Think about it, what is the remainder of dividing any integer by 1? It will always be equal to 0, because 1 can completely divide any number. So rand() % 1 will always produce 0. Then you add 9 to it, so the total result will always come out to be 9.

If you want to produce integers between a and b (inclusive), then use this formula - rand() % (b - a + 1) + a. For example, generating numbers between 50 and 99 will be rand() % 50 + 50

This post has been edited by Neumann: 13 August 2009 - 02:43 PM

Was This Post Helpful? 0

#3 wildgoose   User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 02:37 PM

You need to learn to write functions first
// return a random number between 0...9
int random() 
{
	return rand() % 10;
}



And explain your problem better.

What's ran[4] suppose to be?

This post has been edited by wildgoose: 13 August 2009 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 02:42 PM

It will be much easier to help you if you can provide the full code (at least to the usage of the function) because as wildgoose pointed out, you have syntax errors in the code you provided.
Was This Post Helpful? 0
  • +
  • -

#5 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 02:56 PM

View Postno2pencil, on 13 Aug, 2009 - 01:42 PM, said:

It will be much easier to help you if you can provide the full code (at least to the usage of the function) because as wildgoose pointed out, you have syntax errors in the code you provided.

Uh.. actually I realized I messed up a lot on the code... but I fixed it.
Now the code for assigning a random number is:
firsts[10] = rand() % 10


But I always get 9 still...
Why is that?
Was This Post Helpful? 0
  • +
  • -

#6 wildgoose   User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 02:59 PM

Seeding.
Stick

srand(time(NULL));

at the top of your program.

Can you show your declaration for firsts[] ?

This post has been edited by wildgoose: 13 August 2009 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 03:05 PM

View Postwildgoose, on 13 Aug, 2009 - 01:59 PM, said:

Seeding.
Stick

srand(time(NULL));

at the top of your program.

Can you show your declaration for firsts[] ?

When I put that in my program, I got these errors:
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2143: syntax error : missing ')' before '('
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2143: syntax error : missing ')' before '('
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2091: function returns function
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2143: syntax error : missing ')' before '('
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2091: function returns function
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2091: function returns function
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2143: syntax error : missing '{' before 'constant'
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2059: syntax error : '<Unknown>'
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2059: syntax error : ')'
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2059: syntax error : ')'
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(4) : error C2059: syntax error : ')'



And here is the declaration for firsts[]:
int firsts[16];


Was This Post Helpful? 0
  • +
  • -

#8 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 03:59 PM

Help please?
Was This Post Helpful? 0
  • +
  • -

#9 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

Re: How am I using rand() in C wrong?

Post icon  Posted 13 August 2009 - 04:12 PM

I'm new to programming, and I was just wondering how I could generate a random number between 1 and 10.
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 04:15 PM

I can give you a nudge in the right direction, take a look at this and read through it :)

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Neumann*


Reputation:

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 04:16 PM

Post your complete code.
Was This Post Helpful? 0

#12 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 04:17 PM

Topics merged, please don't create duplicates :)
Was This Post Helpful? 0
  • +
  • -

#13 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 05:10 PM

View PostPsychoCoder, on 13 Aug, 2009 - 03:15 PM, said:

I can give you a nudge in the right direction, take a look at this and read through it :)

[rules][/rules]

I tried to use it, but its really confusing(since I'm just a beginner).
I tried to use the function that he used with #include <stdlib>:
int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
   int n;
   n=random(10);  /* n is random number in range of 0 - 99 */
   return(n);
}


but I get the error:
1>c:\users\jeff\documents\visual studio 2008\projects\cgen2\cgen2\cgen2.c(12) : warning C4013: 'random' undefined; assuming extern returning int
and:
1>cgen2.obj : error LNK2019: unresolved external symbol _random referenced in function _gen_rand
Was This Post Helpful? 0
  • +
  • -

#14 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 05:15 PM

#include <ctime>
int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
   srand(unsigned(time(0)));
   int n;
   n=rand()%100/* n is random number in range of 0 - 99 */
   return(n);
}


Was This Post Helpful? 0
  • +
  • -

#15 jeff544   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-June 09

Re: How am I using rand() in C wrong?

Posted 13 August 2009 - 05:34 PM

View PostImaSexy, on 13 Aug, 2009 - 04:15 PM, said:

#include <ctime>
int gen_rand(void)
/* returns random number in range of 0 to 99 */
{
   srand(unsigned(time(0)));
   int n;
   n=rand()%100/* n is random number in range of 0 - 99 */
   return(n);
}


That doesn't work.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »